【问题标题】:jquery ajax post mix array objects send empty array to phpjquery ajax后混合数组对象将空数组发送到php
【发布时间】:2021-03-14 14:23:59
【问题描述】:

我向 Php 发送了一个数组/对象组合,但 POST 是空的

console.log(数据)

data:
    ABONNR: 900000064
    UMO301NR: 173088
    change:
        assistant: [phone: Array(0)]
        patient: [phone: {…}, social: "single"]
        patient-medical: "Hoge bloeddruk"
        patient-name: "Burger Maria SENSO2ME"
    remove:
        assistant: [{…}, phone: Array(0)]
        patient: [phone: Array(0)]
        patient-medical: "Evenwichtsproblemen"

jQuery

var json = {data:changed}
console.log(json)
 $.ajax({
   url: Settings.base_url + '/home/update',
   data: json, 
   type: 'post',
   success: function (response) {
    console.log(response)
   }
});

php print_r($_POST)

Array ( )

你有什么想法吗? 谢谢!

【问题讨论】:

  • var jsonData = {data:data}data: jsonData, 使用这个..
  • 记录的data有一个密钥change,而在你的AJAX中你尝试发送data:changed。我假设您希望发送data: data.change
  • console.log(json) 给你什么?

标签: php jquery arrays ajax


【解决方案1】:

$_POST[] 不会抓取JSON。您应该改用 file_get_contents()

 // Takes raw data from the request
$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json);

类似的答案:How to get body of a POST in php?

【讨论】:

  • var_dump($data),返回 NULL
  • console.log(json) 给你什么?
  • 见下面的console.log
【解决方案2】:
ABONNR: 900000064,
UMO301NR: 173088,
change:
    assistant: {
        phone: []
    },
    patient:{
        phone:
            [current: "22222222222", prev: ""],
        social: "single",
        patient-medical: "Dementie",
        patient-name: "Burger Maria SENSO2ME"
},
remove:
    assistant: {
        0: {name: "WZC Gitschotelhof Loopwacht+"},
        phone: []
    },
    patient: {
        phone: [],
        patient-medical: "Kan niet alleen recht bij val"
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多