jQuery ajax post数组返回空

时间:2019-10-26 20:29:51

标签: php jquery post

发布具有以下格式的JSON.stringified数组:

[{"AccountNumber":1630,"AccountName":"text1","Balance":83,"AccountType":6,"AccountTypeDescription":"text2"},{"AccountNumber":1930,"AccountName":"text3","Balance":1444492.39,"AccountType":9,"AccountTypeDescription":"text4"}]

在php端,它返回一个空数组。缺少什么?

jQuery编码:

$.ajax({
url: 'url.php',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(resp.responseJSON.Data),
success: function (data2) {
console.log(data2);
},
});

Php:

print_r($_POST);

2 个答案:

答案 0 :(得分:0)

我认为这行有undefined错误:

data: JSON.stringify(resp.responseJSON.Data)

如果您的数组变量名称为resp

var resp=[{"AccountNumber":1630,"AccountName":"text1","Balance":83,"AccountType":6,"AccountTypeDescription":"text2"},{"AccountNumber":1930,"AccountName":"text3","Balance":1444492.39,"AccountType":9,"AccountTypeDescription":"text4"}]

然后尝试:

$.ajax({
url: 'url.php',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(resp),
success: function (data2) {
console.log(data2);
}
});

答案 1 :(得分:-2)

删除数据部分中的JSON.stringify并将响应放入数组中。 喜欢, var urdata = []; urdata.push(resp.responseJSON.Data);

在Ajax通话中: 数据:urdata