如何将ajax数据放入php数组中


how do I get my ajax data into a php array?

我在JS脚本中有以下数据:

$("#holdSave").live('click', function () {
    var arr = {};
    var cnt = 0;
    $('#holdTable tbody tr').each(function () {
        arr[cnt] = {
            buyer: $(this).find('#tableBuyer').html(),
            sku: $(this).find('#tableSku').html(),
            isbn: $(this).find('#tableISBN').html(),
            cost: $(this).find('#tableCost').val(),
            csmt: $(this).find('#tableConsignment').val(),
            hold: $(this).find('#tableHold').val()
        };
        cnt++;
    }); //end of holdtable tbody function
    console.log(arr);
    $.ajax({
        type: "POST",
        url: "holdSave.php",
        dataType: "json",
        data: {
            data: arr
        },
        success: function (data) {
        } // end of success function
    }); // end of ajax call
}); // end of holdSave event

我需要将其发送到一个php文件中,以便更新数据库并通过电子邮件通知更新完成。我的控制台日志(arr);显示了当我在firebug中运行它时的对象,但我无法在php端获得任何信息。以下是我的php代码:

$data = $_POST['data']; 
print_r($data); die;

在这一点上,我只是试图验证是否有信息发送过来。我的print_r($data);不返回任何内容。有人能给我指正确的方向吗?

dataType: "json"意味着您希望在请求中检索json数据,而不是您发送的数据。

如果您想发送一个由$_POST['data']检索的json字符串,请使用

data: {data: JSON.stringify(arr)},

使用json_encode()json_decode()方法

使用下一种方式:

data = {key1: 'value1', key2: 'value2'};
$.post('holdSave.php', data, function(response) {
    alert(response);
});

注意:还没有测试过,一定要查找解析错误。