如何为自定义 Angular $resource 操作指定 headers 参数 [英] How to specify headers parameter for custom Angular $resource action
问题描述
以下工作正常,但我认为这会全局修改 $httpProvider,这不是我想要的.
angular.module('SessionService', ['ngResource']).config(function($httpProvider){$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;字符集=UTF-8'}).factory('登录', 函数($resource){var resource = $resource('/adminui/login',{},{邮政:{方法:POST",isArray:假},});返回资源;})LoginCtrl = function($scope,Login) {$scope.login = function(){Login.post($.param({user:$scope.user.username,password:$scope.user.password}),$.noop,$.noop)}}
无论如何可以这样做吗?
<预><代码>....factory('登录', 函数($resource){var resource = $resource('/adminui/login',{},{邮政:{方法:POST",isArray:假,headers:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'}//忽略},});返回资源;})headers"参数似乎被忽略了.请求还在
Content-Type:application/json;charset=UTF-8
我的标题值是否合适?
我已经确认 1.1.3 确实支持这一点.但是,您需要确保您还获得了 1.1.3 版本的资源服务.快速测试:
angular.module('myApp', ['ngResource']).配置(['$routeProvider',函数($routeProvider){$routeProvider.when('/', {templateUrl: 'partials/partial1.html',controller: 'MyController'});$routeProvider.otherwise({redirectTo: '/'});}]).controller("MyController", function( $scope, Bug) {Bug.post({test:"test"});}).factory('Bug', function($resource){var resource = $resource('/bug',{},{邮政:{方法:POST",isArray:假,headers:{'Content-Type':'application/x-www-form-urlencoded;字符集=UTF-8'}},});返回资源;});
这将发出一个标头设置为(使用 Chrome 确认)的请求:
Content-Type:application/x-www-form-urlencoded;字符集=UTF-8
快速说明,我找不到 angular-resource.js 的下载,所以我不得不去 github 网站下载它.它在这里.
为了一些笑声,我创造了一个小提琴.请注意,POST 调用将失败,但其标头设置正确.小提琴示例
The following works fine, but I am thinking this modifies the $httpProvider globally, which isn't what I want.
angular.module('SessionService', ['ngResource'])
.config(function($httpProvider){
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
})
.factory('Login', function($resource){
var resource = $resource('/adminui/login',{},{
post:{
method:"POST",
isArray:false
},
});
return resource;
})
LoginCtrl = function($scope,Login) {
$scope.login = function(){
Login.post($.param({user:$scope.user.username,password:$scope.user.password}),$.noop,$.noop)
}
}
Is there anyway to do this instead?
...
.factory('Login', function($resource){
var resource = $resource('/adminui/login',{},{
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} // ignored
},
});
return resource;
})
The "headers" parameter seems to be ignored. the request is still
Content-Type:application/json;charset=UTF-8
Is my value for headers ok?
I have confirmed that 1.1.3 does indeed support this. However, you need to make sure you also get the 1.1.3 version of the resource service. A quick test of:
angular.module('myApp', ['ngResource']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {templateUrl: 'partials/partial1.html',controller: 'MyController'});
$routeProvider.otherwise({redirectTo: '/'});
}])
.controller("MyController", function( $scope, Bug) {
Bug.post({test:"test"});
})
.factory('Bug', function($resource){
var resource = $resource('/bug',{},{
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
},
});
return resource;
});
This will make a request with the headers set to (confirmed using Chrome):
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
A quick note, I was unable to find a download of the angular-resource.js, so I had to go the the github website to download it. It is here.
For some giggles, I created a fiddle. Notice that there will be a failed POST call, but its headers are set correctly. Example Fiddle
这篇关于如何为自定义 Angular $resource 操作指定 headers 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!