[英]AngularJS, How to correctly modify http headers in a factory
我希望Angular大師可以指出正確的方向。 使用以下格式的工廠時,如何修改POST的標頭,以便:
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'?
編碼:
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);
}
};
}]);
我之前嘗試過類似的方法。 該方法看起來像
$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
});
}
為什么不簡單地使用$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.