2017-09-08 43 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