我想用chrome dev console來調試我的角度應用程序。我想從角度向本地服務器發送一個get請求。我已經試過如下:如何使這個Angular http獲取請求(與基本認證)工作?
$http = angular.element($0).injector().get('$http');
$base64 = angular.element($0).injector().get('$base64');
var auth = $base64.encode("user:passwd");
var authHeaders = {"Authorization": "Basic " + auth,"Access-Control-Allow-Origin":"*"};
$http.get("url",{headers:authHeaders,method:"GET"})
閱讀這個答案後: https://stackoverflow.com/a/30296149/1496826
我想自定義標題是問題。所以,我試圖把授權頭在身:
$http.get("url",{data: {"Authorization": "Basic " + auth,"Access-Control-Allow-Origin":"*"},method:"GET"})
但我仍然得到同樣的錯誤:
XMLHttpRequest cannot load "url". No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.
此相同的GET請求工作正常,從郵差:
var settings = {
"async": true,
"crossDomain": true,
"url": "url",
"method": "GET",
"headers": {
"authorization": "Basic bWdhcasdasd",
"cache-control": "no-cache",
"postman-token": "XXXXXX-XXXXXX-xXXXX-xXXXX"
}
}
我已經嘗試了幾個其他的變化像 - $ http default/common header等,但一切都失敗了。請幫忙。
您是否嘗試過無需驗證的簡單請求以查看其是否有效?像:$ http({method:'GET',url:'/ someUrl' })。(函數successCallback(response){0},函數errorCallback(response){}); – ukn