提交带参数的数据
httpPost: function (data, url) {
var deferred = $q.defer();
$http({
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: data
}).success(
function (data, status, header, config) {
deferred.resolve(data);
}).error(function(data, status, headers, config) {;
errShow(data, status, headers, config);
//方法
});
return deferred.promise;
}, 提交图片
httpPostImg: function (data, url) {
var deferred = $q.defer();
$http({
method: 'POST',
url: url,
headers: {'Content-Type': undefined},
transformRequest: angular.identity,
data: data,
processData: false,
contentType: false,
}).success(
function (data, status, header, config) {
deferred.resolve(data);
}).error(function(data, status, headers, config) {
errShow(data, status, headers, config);
});
return deferred.promise;
},
HTTP POST 请求详解

本文介绍了两种使用 AngularJS 的 $http 服务发起 POST 请求的方法:一种用于发送普通表单数据,另一种用于上传图片文件。详细解释了如何设置请求头、转换请求体及处理响应。
266

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



