3

我想获取 RESTful API 的“X-Total-Count”响应标头。在尝试获取我的 ngResource 'User' 的查询回调函数中的标头时,似乎 $http 忽略了很多响应标头。

这些是我的请求的响应标头:

Access-Control-Allow-Origin: *
Cache-Control: max-age=0, private, must-revalidate
Connection: close
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Fri, 17 Oct 2014 11:13:26 GMT
Link: <http://xxxx.xxx/user?page=2>; rel="next"
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Total-Count: 32

在这里,我正在查询一组用户:

User.query({
    page: $scope.pagingOptions.currentPage, 
    limit: $scope.pagingOptions.pageSize
}, function(users, responseHeaders) {
    console.log(responseHeaders());
    console.log(responseHeaders('X-Total-Count'));
});

这是控制台的结果:

Object { cache-control="max-age=0, private, must-revalidate", content-type="application/json; charset=utf-8"}
null

那么为什么 $http 的 responseHeaders() 函数只转换 10 个标头属性中的 2 个呢?

4

1 回答 1

0

你确定你不是在做跨域资源共享吗?

参见7.1.1 处理对跨域请求的响应

该规范禁止访问除简单响应头字段(即 Cache-Control、Content-Type...)以外的任何响应头字段

于 2015-01-30T17:11:07.507 回答