Post 使用 $resource 形成数据
Post form data with $resource
以下代码 post 将数据形成为 json 而不是键值对。
服务
$resource(apiPath, {
Id: '@Id',
apt_id: user_info.apt_id,
mauth_token: user_info.mauth_token,
mauth_account_id: user_info.mauth_acnt_id,
rest: 1,
}, {
save:{
method:'POST',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}
});
控制器
.controller('BroadcastSmsSendCtrl', function($scope, $ionicLoading, $ionicActionSheet, $state, $timeout, $location, BroadcastSmsSvcs, GetUserCountHttpSvcs) {
$scope.entry = new BroadcastSmsSvcs();
$scope.doSubmit = function() {
BroadcastSmsSvcs.save({}, $scope.entry);
};
});
尝试将其添加到您的 $resource 对象中
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("&");
}
}
在您的 .config()
函数中添加以下内容
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
var key, result = [], response;
if(typeof data == "string") { //$http support
response = data;
} else {
for (key in data) {
if (data.hasOwnProperty(key)) {
result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
}
}
response = result.join("&");
}
return response;
});
AngularJs 提供转换为 jQuery 类似序列化对象的服务。
而不是json序列化:{"id": 1, "name": "whatever"}
变成:id=1&name=whatever
所以你可以简单地做到:
save:{
method: "POST",
headers : {"Content-Type": "application/x-www-form-urlencoded"},
transformRequest: function(data) {
return $httpParamSerializerJQLike(data);
}
}
注意:在你的服务中你必须注入$httpParamSerializerJQLike
服务
以下代码 post 将数据形成为 json 而不是键值对。
服务
$resource(apiPath, {
Id: '@Id',
apt_id: user_info.apt_id,
mauth_token: user_info.mauth_token,
mauth_account_id: user_info.mauth_acnt_id,
rest: 1,
}, {
save:{
method:'POST',
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}
});
控制器
.controller('BroadcastSmsSendCtrl', function($scope, $ionicLoading, $ionicActionSheet, $state, $timeout, $location, BroadcastSmsSvcs, GetUserCountHttpSvcs) {
$scope.entry = new BroadcastSmsSvcs();
$scope.doSubmit = function() {
BroadcastSmsSvcs.save({}, $scope.entry);
};
});
尝试将其添加到您的 $resource 对象中
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("&");
}
}
在您的 .config()
函数中添加以下内容
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
var key, result = [], response;
if(typeof data == "string") { //$http support
response = data;
} else {
for (key in data) {
if (data.hasOwnProperty(key)) {
result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
}
}
response = result.join("&");
}
return response;
});
AngularJs 提供转换为 jQuery 类似序列化对象的服务。
而不是json序列化:{"id": 1, "name": "whatever"}
变成:id=1&name=whatever
所以你可以简单地做到:
save:{
method: "POST",
headers : {"Content-Type": "application/x-www-form-urlencoded"},
transformRequest: function(data) {
return $httpParamSerializerJQLike(data);
}
}
注意:在你的服务中你必须注入$httpParamSerializerJQLike
服务