[英]AngularJS, How to correctly modify http headers in a factory
I am hoping an Angular guru can point me in the right direction. 我希望Angular大师可以指出正确的方向。 When using a factory in the format below, how do I modify the header for POST so that:
使用以下格式的工厂时,如何修改POST的标头,以便:
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'?
The code: 编码:
var tableModule = angular.module('theApp', ['ui-rangeSlider','ngScrollbar']);
tableModule.factory('Services', ['$http',
function($http) {
return {
get : function($path, callback) {
return $http.get($path).success(callback);
},
post : function($path, $data, callback) {
return $http.post($path, $data).success(callback);
}
};
}]);
I tried something like this earlier. 我之前尝试过类似的方法。 The method would look like
该方法看起来像
$scope.update = function (user) {
$http({
method: 'POST',
url: 'https://mytestserver.com/that/does/not/exists',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
transformRequest: function (data) {
var postData = [];
for (var prop in data)
postData.push(encodeURIComponent(prop) + "=" + encodeURIComponent(data[prop]));
return postData.join("&");
},
data: user
});
}
Also see my fiddle http://jsfiddle.net/cmyworld/doLhmgL6/ 另请参阅我的小提琴http://jsfiddle.net/cmyworld/doLhmgL6/
Why not simply use the third config argument to $http.post
... 为什么不简单地使用
$http.post
的第三个配置参数...
post: function(path, data) {
return $http.post(path, data, {
headers: {
'Content-type': 'application/x-www-form-urlencoded'
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.