如何使用 $resource 操作设置自定义标题? [英] how to set custom headers with a $resource action?
本文介绍了如何使用 $resource 操作设置自定义标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 $http,我们可以这样做:
with $http, we can do this:
var config = { headers: { 'something': 'anything' } };
$http.get('url/to/json', config)
.success(function() {
// do something…
})
我想对 $resource 引用做同样的事情(不起作用):
i would like to do the same with a $resource reference (not working):
var config = { headers: { 'something': 'anything' } };
MyResource.get(
config,
function() { // success
// do something…
}
);
相应的服务声明如下:
.factory('MyResource', function($resource){
return $resource('url/to/json');
})
它不起作用:配置对象转到 url 而不是在 http 标头中.
it's not working : the config object goes to the url and not in the http headers.
有没有办法做到这一点?
Is there a way to do that ?
推荐答案
headers
for $resource
从 AngularJS 1.1.1 开始可用.确保您使用了正确的版本.
headers
for $resource
is available since AngularJS 1.1.1. Make sure you have correct version used.
格式为
$resource('url/to/json', {}, {headers: { 'something': 'anything' }});
[祖玛编辑]上面好像不太对.$resource 的第三个参数应该不同.这对我来说似乎更正确:
[edit by zuma] The above doesn't seem right. The third parameter to $resource should be a different. This seems more correct to me:
$resource('url/to/json', {}, {
get: {
method: 'GET',
headers: { 'something': 'anything' }
}
});
这篇关于如何使用 $resource 操作设置自定义标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文