我愿意检索资源请求的响应头,因为我在其中放入了分页信息和其他内容而不是响应主体,以使REST api清晰.
虽然我们可以从成功/错误回调中获取它,如下所示:
Object.get({type:'foo'}, function(value, responseHeaders){
var headers = responseHeaders();
});
Run Code Online (Sandbox Code Playgroud)
'Object'是我的资源工厂服务.
此外,当我尝试在解决所需资源后更改路由时,我尝试过:
.when('/list', {
templateUrl: 'partials/list.html',
controller: 'ListCtrl',
// wait for the required promises to be resolved before controller is instantialized
resolve: {
objects: ['Object', '$route', function(Object, $route){
return Object.query($route.current.params).$promise;
}]
}
})
Run Code Online (Sandbox Code Playgroud)
并且在控制器中,只需注入"对象"而不是对象服务,因为它已经解析并填充了真实数据.
但是当我尝试从控制器中的"对象"获取头信息时,我遇到了问题.
我试过了objects.$promise.then(function(data, responseHeaders){}),但是responseHeader是未定义的.
如何更改$ resource服务的行为,以便将responseHeader getter抛出到$ promise then()回调函数中?
我的服务"对象"供参考:
myServices.factory('Object', ['$resource',
function($resource){
return $resource('object/:id', {id: '@id'}, {
update: {method: 'PUT'},
});
}
]);
Run Code Online (Sandbox Code Playgroud) 我正在使用$ 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
Run Code Online (Sandbox Code Playgroud)
但我无法从服务器获得完整的标头.
这是我的代码:
.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);
});
Run Code Online (Sandbox Code Playgroud)