I would like to know how to pass headers to AngularJS $resource method
Here is the factory method
.factory('DataRepository', function ($resource) {
return $resource(serviceUrlPrefix + '/api/v1/AppList/:id', { id: '@id' }, { 'query': { method: 'GET', isArray: false }, 'update': { method: 'PUT', AppList: '@req', headers: { 'X-Requested-With': 'XmlHttpRequest' } } });
});
Here is the call to the dataRepository
dataRepository.update({ id: req[uniqueIDColumn] }, req, function (data) {
},
function (error) {
});
This code works fine. But i have few queries
Question 1:
Rather than specifying the headers in the factory method , how can i specify it in the call to the factory method? I tried few methods but it didnt work out.
Question 2:
I specified the header in the update method in the factory. When i perform "Save" using that factory, that header has been taken by default. But i have specified it explicitly for PUT method. Right? Why and how?
Question 3:
If i would like to specify the header for the particular factory in common for all Http methods, what is the way to do it?
Question 4:
What is the nomenclature for passing the parameters and the significance of "@" symbol before parameter and also in the below part, AppList is the parameter name used in the WebAPI, is it mandatory that it should match the parameter name in the WebAPI method, if its not matching, its not working:(
AppList: '@req'
这篇博客探讨了在AngularJS中使用资源服务($resource)如何处理HTTP头信息。作者提出了四个问题:如何在调用时而非在工厂方法中指定头信息?为什么在更新方法中设置的头信息会默认应用?如何为整个工厂设置通用的HTTP头信息?以及参数传递中的'@'符号含义和参数匹配规则。内容涉及AngularJS服务配置和HTTP请求的细节。
173

被折叠的 条评论
为什么被折叠?



