如何阅读与$资源响应头? [英] How to read response headers with $resource?
本文介绍了如何阅读与$资源响应头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用$资源从我的RESTful服务中获取数据,我需要读取响应头获得X-页'和'X与总页数的分页值。
示例:
访问控制,最大年龄:1728000
缓存控制:最大年龄= 0,民营,必重新验证
连接:保持活动
内容长度:2637
内容类型:应用程序/ JSON
日期:星期四,2014年4月10日16时53分01秒GMT
服务器:使用WEBrick / 1.3.1(红宝石/ 2.1.1 / 2014年2月24日)
有所不同:产地
的X页面:1
的X每页:10
的X总:17
的X总-页数:2
但我无法从服务器获取完整标题。
这是我的code:
.factory('TestAPI',['$资源',
功能($资源){
返回$资源(http://ip.jsontest.com/?callback=showIP,{},{
查询:{
方法:GET
}
});
}])TestAPI.query({},功能(价值,responseHeaders响应){
的console.log(responseHeaders响应());
},函数(响应){
的console.log(响应);
});
解决方案
在你的响应头,你必须添加以下标题:
访问控制展露报头:X-总的页面,X页
由于这一点,浏览器能够暴露你的报头关的阅读角。
I'm using $resource to get data from my RESTful service and I need to read response headers to get 'X-Page' and 'X-Total-Pages' value for pagination.
Example:
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
But I couldn't get full headers from server.
This is returned headers:
This is the headers from server:
This is my code:
.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);
});
解决方案
In your response headers you have to add the following header:
Access-Control-Expose-Headers: X-Total-Pages, X-Page
With this, the browser is capable to expose your customs headers an read it angular.
这篇关于如何阅读与$资源响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文