2017-09-08 104 views
0

我有一个REST Web服务需要基本身份验证才能使用其数据。我正在AngularJS中使用它。如何在AngularJS的休息服务中传递基本认证?

我这样做是如下的方式:

$http.get('http://localhost:9200/persons/_search?_source=Gender', { 
     headers: { 
      withCredentials: true, 
      headers:{ 'Authorization': 'Basic ' + btoa("admin" + ":" + "admin")} 

     } 
    }). 
     then(function(response) { 
      ctrl.json = response.data; 

     });  
}]); 

但是我在控制台时遇到的错误是:

请求头字段标题不被 访问 - 允许预检响应中的Control-Allow-Headers。

有人可以帮助解决这个问题吗?请注意,我已经在邮递员上进行过测试,并且工作正常,所有数据都正确返回给我。

回答

0

这是一个CORS问题,您必须允许来自您的服务器的跨源请求

+0

我已经允许。但问题依然存在。任何替代品? – jones

+0

你是怎么答的? – Sletheren

+0

http.cors.enabled:true http.cors.allow-origin:“*” http.cors.allow-methods:OPTIONS,HEAD,GET,POST,PUT,DELETE http.cors.allow-headers:“ X-Requested-With,Content-Type,Content-Length,X-User“ 我在配置文件中添加了上述行。 – jones