Rumah > Soal Jawab > teks badan
我是这样提交表单的
$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
Masalah selesai
Sebab:
Secara lalai, jQuery menghantar data menggunakan Content-Type: x-www-form-urlencoded dan siri foo=bar&baz=moe yang biasa, bagaimanapun, menghantar data menggunakan Content-Type: application/json dan { "foo": "bar. ", "baz": "moe" } Siri JSON
Secara lalai, jQuery menghantar data menggunakan Content-Type: x-www-form-urlencoded dan urutan yang serupa dengan "foo=bar&baz=moe", walau bagaimanapun AngularJS, menghantar data menggunakan Content-Type: application/json dan { " foo": "bar", "baz": "moe" } jujukan json sedemikian.
Penyelesaian
.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
Mungkin anda sudah biasa menggunakan jquery,
jquery merangkum operasi query_string
Cuma tambah kod ini pada sudut dan tukar nama contoh
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. Baca dokumentasi rasmi angularjs untuk memahami penggunaannya Nampaknya pengepala $http adalah sedikit berbeza.
2. Muat turun alat fiddler dan lihat maklumat seperti pengepala permintaan dan param.
伊谢尔伦2017-05-15 16:54:15
Pelayan tidak boleh mendapatkannya secara langsung menggunakan $_REQUEST/$_POST, tetapi perlu menggunakan:
$params = json_decode(file_get_contents('php://input'),true);
Untuk mendapatkan data penyerahan