首页 > 解决方案 > jquery ajax后混合数组对象将空数组发送到php

问题描述

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

控制台日志(数据)

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 ( )

你有想法吗 ?谢谢!

标签: phpjqueryarraysajax

解决方案


$_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);

那里有类似的答案:如何在 php 中获取 POST 的正文?


推荐阅读