【问题标题】:jquery ajax post array returns emptyjquery ajax post数组返回空
【发布时间】:2019-10-26 20:29:51
【问题描述】:

使用以下格式发布 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);

【问题讨论】:

  • 使用file_get_contents('php://input')从请求中提取json字符串
  • 试试 var_dump($_POST) 而不是 print_r 看看你得到了什么?

标签: php jquery post


【解决方案1】:

我认为这行有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);
}
});

【讨论】:

    【解决方案2】:

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

    在 Ajax 调用中: 数据:urdata

    【讨论】:

    • 如果以'application/json' 发送则不会
    • 我不同意。您设置内容类型或发送序列化数据
    • 对...但是 OP 已经在设置 ​​contentType。所以,如果你想让它们序列化,你需要澄清而不是混合两者
    • 操作?所以,最好不要连载。另外,他想要数组中的数据,但他没有发送数组
    • OP = Original Post .... 显示的数据在我看来确实像数组
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多