2014-04-10 64 views
7

我正在使用$ resource從我的RESTful服務獲取數據,並且需要讀取響應標題才能獲得分頁的「X-Page」和「X-Total-Pages」值。

例子:

Access-Control-Max-Age:1728000 
Cache-Control:max-age=0, private, must-revalidate 
Connection:Keep-Alive 
Content-Length:2637 
Content-Type:application/json 
Date:Thu, 10 Apr 2014 16:53:01 GMT 
Server:WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24) 
Vary:Origin 
X-Page:1 
X-Per-Page:10 
X-Total:17 
X-Total-Pages:2 

但我無法從服務器獲取完整標題。

這回的標題: enter image description here

這是從服務器的標題: enter image description here

這是我的代碼:

.factory('TestAPI', ['$resource', 
     function ($resource) { 
      return $resource("http://ip.jsontest.com/?callback=showIP", {}, { 
       query: { 
        method: 'GET' 
       } 
      }); 
     }]) 

TestAPI.query({}, function (value, responseHeaders) { 
       console.log(responseHeaders()); 
      }, function (response) { 
       console.log(response); 
}); 

回答

9

在你的響應頭,你必須添加以下標題:

Access-Control-Expose-Headers: X-Total-Pages, X-Page 

使用此功能,瀏覽器可以將您的報頭顯示爲角度讀取。

+1

但是如何讀取它的角? –

+1

這救了我的命!非常感謝你。 –

+1

請問如何獲得相關幫助 –

相關問題