使用$ http,我们可以这样做:
var config = { headers: { 'something': 'anything' } };
$http.get('url/to/json', config)
.success(function() {
// do something…
})
Run Code Online (Sandbox Code Playgroud)
我想用$ resource引用(不工作)做同样的事情:
var config = { headers: { 'something': 'anything' } };
MyResource.get(
config,
function() { // success
// do something…
}
);
Run Code Online (Sandbox Code Playgroud)
使用相应的服务声明如下:
.factory('MyResource', function($resource){
return $resource('url/to/json');
})
Run Code Online (Sandbox Code Playgroud)
它不起作用:config对象转到url而不是http头.
有没有办法做到这一点 ?
zs2*_*020 82
headers
对于$resource
是因为AngularJS 1.1.1可用.确保使用的版本正确.
格式是
$resource('url/to/json', {}, {headers: { 'something': 'anything' }});
Run Code Online (Sandbox Code Playgroud)
[zuma编辑]以上看似不对.$ resource的第三个参数应该是不同的.这似乎对我来说更正确:
$resource('url/to/json', {}, {
get: {
method: 'GET',
headers: { 'something': 'anything' }
}
});
Run Code Online (Sandbox Code Playgroud)
Moo*_*tom 18
headers
资源操作中的对象既支持static
其字段的dynamic
值,也支持从函数返回的值.
$resource('url/to/json', {}, {
get: {
method: 'GET',
headers: {
'header_static': 'static_value',
'header_dynamic': dynamicHeaderVal
}
}
});
function dynamicHeaderVal(requestConfig){
// this function will be called every time the "get" action gets called
// the result will be used as value for the header item
// if it doesn't return a value, the key will not be present in the header
}
Run Code Online (Sandbox Code Playgroud)
Nit*_*mar 15
演示代码
angular.module('Test',['ngResource'])
.controller('corsCtrl', function ($scope, $http, MyResource) {
$http.defaults.headers.common['test']= 'team'; //Using $http we can set header also
MyResource.get();
})
.factory('MyResource', function($resource) { //Services
return $resource('url/to/json');
})
Run Code Online (Sandbox Code Playgroud)
see in Request Header
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
67839 次 |
最近记录: |