使用 POST jquery 发送数组(对象)


Send array (object) with a POST jquery

var data = {
    'ids': $("input[name='ids''['']']").map(function(){return $(this).val();}),
    'price': $("input[name='price''['']']").map(function(){return $(this).val();})
};
alert(data);

$.post("api/update_prices.php", {'data[]': data}, function (responseText) {
    alert(responseText);
});

或。。。

$.post("api/update_prices.php", data, function (responseText) {
    alert(responseText);
});

警报数据正在输出对象(对象(。我正在寻找一个堆栈溢出,但它仍然不起作用。alert(responseText( 永远不会被调用。

您是否尝试在jQuery Ajax API中将内容类型指定为"application/json",然后调用

JSON.stringify(data(;

另外,在Google Chrome浏览器中打开Web开发人员控制台并导航到"网络"选项卡以查看Ajax调用期间发生的情况,即在Ajax调用中发送哪些数据和接收哪些数据。

你试过serialize吗?它汇总了所有表单数据。这似乎是您要尝试对data对象执行的操作。

$.post("api/update_prices.php", $(':input').serialize(), function (responseText) {
    alert(responseText);
});

http://api.jquery.com/serialize/

你的 $.post 函数应该是这样的:

$.post("api/update_prices.php", {'data[]': JSON.stringify(data)}, function (responseText)  {
    alert(responseText);
});

注意 JSON.stringify