[英]With AngularJS, I don't want to set the global $http.defaults.headers.common. Can I send my custom header with each $resource call?
I'm calling a back-end server that I cannot control. 我正在调用一个我无法控制的后端服务器。 Currently it's using jQuery ajax like this: 目前它正在使用这样的jQuery ajax:
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 我想将它转换为使用$resource
服务,并让它工作
$http.defaults.headers.common['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. 唯一的问题是我必须使用auth凭据设置全局$http
服务默认值。
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). 我看到你应该能够通过$http
调用传递自定义标头,现在使用$resource
调用,但是我找不到任何关于如何在我的情况下执行此操作的示例(使用POST) 。
I also can't find anything on this in the AngularJS documentation. 我在AngularJS文档中也找不到任何相关内容。 How do you guys figure this stuff out? 你们怎么想出这个东西? The docs are so bad! 文档非常糟糕!
Instead of this: 而不是这个:
$http.defaults.headers.common['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 看一下angular-resource.js中$ resource源代码中的DEFAULT_ACTIONS列表
The $resource documentation does cover it, though its certainly awkward to read. $ resource文档确实涵盖了它,尽管阅读肯定很难。 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. 您必须为其执行自定义操作,并且您可以传递给配置的所有参数都不会列在$ resource页面上。 You'll want to check out the $http docs page for the possibilities for the config object. 您需要查看$ http docs页面,了解配置对象的可能性。
$resource()
takes 3 arguments: the URL, the default parameters object, and the actions object. $resource()
有3个参数:URL,默认参数对象和actions对象。 Actions map method names to $http configs, basically. 动作基本上将方法名称映射到$ http configs。
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). 这些操作转换为MyResource对象上的方法,因此如果您愿意,您可以将操作命名为更加语义而不仅仅是“发布”(例如,文档页面上的示例为信用卡资源设置了“收费”操作,例如)。
The documentation for $http is pretty solid, as is most of the other documentation on their site. $ http的文档非常可靠,与其网站上的大多数其他文档一样。
And you can absolutely define your auth headers on each individual AJAX calls. 并且您可以在每个单独的AJAX调用上绝对定义您的auth标头。
try something like this: 尝试这样的事情:
$http({
method: 'POST',
url: 'serverUrl',
data: parameters,
headers: { Authorization: cred }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.