我是這樣提交表單的
$http({
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
url: url,
data: {
type: 1
}
})
為什麼我的伺服器接到的會是這樣的東西?
{ '{"type":1}': '' }
ringa_lee2017-05-15 16:54:15
問題解決了
原因:
By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: appli/json and { "foo" ", "baz": "moe" } JSON serialization
預設情況下,jQuery傳輸資料使用Content-Type: x-www-form-urlencodedand和類似"foo=bar&baz=moe"的序列,然而AngularJS,傳輸資料使用Content-Type: application/json和{ "foo ": "bar", "baz": "moe" }這樣的json序列。
解決方法
.config(function($httpProvider) {
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function(data) {
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function(obj) {
var query = '';
var name, value, fullSubName, subName, subValue, innerObj, i;
for (name in obj) {
value = obj[name];
if (value instanceof Array) {
for (i = 0; i < value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
} else if (value instanceof Object) {
for (subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
} else if (value !== undefined && value !== null) {
query += encodeURIComponent(name) + '='
+ encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
};
return angular.isObject(data) && String(data) !== '[object File]'
? param(data)
: data;
}];
})
PHPz2017-05-15 16:54:15
可能是你jquery用習慣了,
jquery封裝了query_string操作
你在angular加上這段程式碼就好了,改下範例名稱
APP.config(['$httpProvider',function($httpProvider) {
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function(data) {
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function(obj) {
var query = '';
var name, value, fullSubName, subName, subValue, innerObj, i;
for (name in obj) {
value = obj[name];
if (value instanceof Array) {
for (i = 0; i < value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
} else if (value instanceof Object) {
for (subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
} else if (value !== undefined && value !== null) {
query += encodeURIComponent(name) + '='
+ encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
};
return angular.isObject(data) && String(data) !== '[object File]'
? param(data)
: data;
}];
}]);
阿神2017-05-15 16:54:15
1、看angularjs的官方文件了解其用法,看起來像$http的header有點不同。
2.下載fiddler工具,查看請求header和params等資訊。
伊谢尔伦2017-05-15 16:54:15
服務端不能直接用 $_REQUEST/$_POST取得到,需要用:
$params = json_decode(file_get_contents('php://input'),true);
來取得提交資料