admin管理员组文章数量:1355600
I'm calling a back-end server that I cannot control. Currently it's using jQuery ajax like this:
return $.ajax({
type: "POST",
url: "/api/cases/store",
contentType: "application/json",
data: JSON.stringify(parameters),
headers: { "Authorization": cred } : {}
}) // ... etc.
I want to convert it to use the $resource
service, and got it working doing this
$http.defaults.headersmon['Authorization'] = cred;
return $resource('/api/cases/store').save();
The only problem is that I'm having to set the global $http
service defaults with the auth credentials.
I am seeing that you're supposed to be able to pass in custom headers with the $http
call, and now with $resource
calls, but I can't find any examples of how to do it in my case (with a POST).
I also can't find anything on this in the AngularJS documentation. How do you guys figure this stuff out? The docs are so bad!
I'm calling a back-end server that I cannot control. Currently it's using jQuery ajax like this:
return $.ajax({
type: "POST",
url: "/api/cases/store",
contentType: "application/json",
data: JSON.stringify(parameters),
headers: { "Authorization": cred } : {}
}) // ... etc.
I want to convert it to use the $resource
service, and got it working doing this
$http.defaults.headers.mon['Authorization'] = cred;
return $resource('/api/cases/store').save();
The only problem is that I'm having to set the global $http
service defaults with the auth credentials.
I am seeing that you're supposed to be able to pass in custom headers with the $http
call, and now with $resource
calls, but I can't find any examples of how to do it in my case (with a POST).
I also can't find anything on this in the AngularJS documentation. How do you guys figure this stuff out? The docs are so bad!
Share Improve this question edited Mar 30, 2014 at 22:35 Daisha Lynn asked Mar 30, 2014 at 21:53 Daisha LynnDaisha Lynn 4591 gold badge4 silver badges16 bronze badges3 Answers
Reset to default 7Instead of this:
$http.defaults.headers.mon['Authorization'] = cred;
return $resource('/api/cases/store').save();
Do this:
return $resource('/api/cases/store', {}, {
save: {
method: 'POST',
headers: { 'Authorization': cred }
}
}).save();
Note that you have to use 'save' as the action, the first key in the third parameter. Can't test it, so let me know if it works.
And I agree. The documentation doesn't talk about it. Take a look at the DEFAULT_ACTIONS list in the $resource source-code in angular-resource.js
The $resource documentation does cover it, though its certainly awkward to read. You have to make a custom action for it, and all the parameters you can pass to the config are NOT listed on the $resource page. You'll want to check out the $http docs page for the possibilities for the config object.
$resource()
takes 3 arguments: the URL, the default parameters object, and the actions object. Actions map method names to $http configs, basically.
You want to make a custom action, something like:
var MyResource = $resource('/myendpoint/', {}, { 'post': { method: 'POST', headers: {"Authorization" : cred}}); // and any other $http options you might want
Those actions get turned into methods on the MyResource object, so you could name the action something more semantic than just "post" if you wanted (the examples on the docs page set up a "charge" action for a credit card resource, for instance).
The documentation for $http is pretty solid, as is most of the other documentation on their site.
And you can absolutely define your auth headers on each individual AJAX calls.
try something like this:
$http({
method: 'POST',
url: 'serverUrl',
data: parameters,
headers: { Authorization: cred }
});
本文标签:
版权声明:本文标题:javascript - With AngularJS, I don't want to set the global $http.defaults.headers.common. Can I send my custom header w 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743983559a2571181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论