为什么我不能在jQuery ajax / post中发送数据数组?

时间:2021-11-13 07:13:41

I have this JavaScript source:

我有这个JavaScript源代码:

var mydata = [];

$('.myManyElements').each(function() {
    mydata.push({
        'id': $(this).data('id'),
        'quantity': $(this).data('quantity'),
        'price': $(this).data('price'),
        'price_total': Order.getTotalPrice()
    });
});

$.post('/archive', mydata, function(data) {
    if(data.success) {
        alert(data.response);
    } else {
        alert('Custom Error Report!');
    }
}, 'json');

And in my /archive request I have this sample PHP:

在我的/ archive请求中我有这个示例PHP:

echo json_encode(array(
    'success' => true,
    'response' => print_r($_POST, true),
));

When I check Firebug's NET panel for XHR, in the POST tab it says that I've sent this:

当我检查Firebug的XHR的NET面板时,在POST选项卡中它说我发送了这个:

undefined=

When I get my response in my alert it outputs:

当我在警报中得到我的回复时,它会输出:

Array
(
    [undefined] => 
)

Why can't I send an array of data for my POST request?

为什么我不能为我的POST请求发送数据数组?

1 个解决方案

#1


1  

Try

$.post('/archive', {'mydata': JSON.stringify(mydata)}, function(data)

#1


1  

Try

$.post('/archive', {'mydata': JSON.stringify(mydata)}, function(data)