我有一個angularjs 1.x應用程序使用jsons與休息服務進行通信。angularjs httpprovider攔截器無法訪問自定義http響應頭(CORS請求)的原因是什麼?
由於服務位於不同的域下,因此我使用CORS。
我角應用程序有記錄的響應頭的響應攔截:
app.factory('responseInterceptor', ["$q", function ($q) {
return {
response: function (response) {
console.log(response.headers()); // log the headers!!!
return response || $q.when(response);
},
responseError: function (rejection) {
return $q.reject(rejection);
}
}
}]).
config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('responseInterceptor');
}]);
現在一切工作正常,但我不能訪問自定義HTTP響應頭
我使用HTTP嗅探器工具,我可以看到服務器響應中的標題。
我敢肯定,這與CORS有關,但我不能指摘它。
的響應頭文件(包括CORS)是這樣的:
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
MY-CUSTOM-HEADER: Go Home This Works
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Headers: MY-CUSTOM-HEADER, CONTENT-TYPE
Date: Tue, 21 Jun 2016 13:18:20 GMT
Content-Length: 14112
這是我在控制檯中看到自攔截:
Object {pragma: "no-cache", content-type: "application/json; charset=utf-8", cache-control: "no-cache", expires: "-1"}
問題:
是什麼原因我在登錄響應頭時看不到MY-CUSTOM-HEADER
?或者我可以在我的嗅探工具上看到的其他http響應標頭。誰阻止我在responseInterceptor
上訪問它們?
編輯: 我檢查了同一個域下的同一個應用程序,並且可以使用相同的代碼訪問這個頭。它確實與CORS有關。