Ajax请求.服务器返回null


Ajax request. Server returned null

JavaScript文件#form-html表单.在var数据-对象

    $(document).ready(function() {
      $('#form').submit(function (e) {
        e.preventDefault();
        var data = $('#form').serializeArray();
        $.ajax({
            type: "POST",
            url: "... .php",
            data: data,
            dataType: "json",
            success: function(d) {
                ...
            },
            error: function(xhr, status, error) {
                alert(xhr.responseText + '|'n' + status + '|'n' +error);
            }
        });
    }); 
}); 

Php文件

$data =  json_decode($_POST['data']);
$dataJson = json_encode($data);
echo $dataJson;

服务器返回Null。为什么?

为什么要解码$_POST['data']?您的所有表单字段都有前缀data

试试这个

$data = json_decode($_POST, true); // parse to array
echo json_encode($data);