2016-06-21 53 views
1

我有一個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有關。

回答

0

好,標識,這個問題是CORS後,我發現我失蹤的CORS HTTP頭叫Access-Control-Expose-Headers

設置Access-Control-Expose-Headers="MY-CUSTOM-HEADER"後,我能夠訪問頭。

相關問題