javascript 具有多个数组数据参数的 jQuery AJAX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10886848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:21:44  来源:igfitidea点击:

jQuery AJAX with Multiple Array data Parameters

javascriptjqueryajax

提问by Kevin_TA

I've successfully posted a single array, but I can't figure out how to send more than one array in an AJAX post. Here is my code for one array:

我已经成功发布了一个数组,但我不知道如何在 AJAX 帖子中发送多个数组。这是我的一个数组的代码:

var a = new Array();
// fill array
var a_post = {};
a_post['array1[]'] = a;

$.ajax({
    url: "submitOrder.php",
    data: a_post,
    type: 'post',
    success: function(data) {
        alert(data);
    }
});

And in submitOrder.php I have:

在 submitOrder.php 我有:

$array1= $_POST['array1'];

foreach ($array1 as $a => $b)
echo "$array1[$a] <br />";

This works fine. However, when I try to add a second array b_postto the data:field, it doesn't work. I tried data: {a_post, b_post},and a few variations of that, but I can't get it to work properly. While I'm at it, how would I then load submitOrder.phpafter posting rather than show an alert of the data?

这工作正常。但是,当我尝试b_post向该data:字段添加第二个数组时,它不起作用。我尝试data: {a_post, b_post},了一些变体,但我无法让它正常工作。当我在做时,我将如何submitOrder.php在发布后加载而不是显示数据警报?

UPDATE

更新

Using Nicolas' suggestion, I got this to work changing the data field to:

使用 Nicolas 的建议,我可以将数据字段更改为:

data: {'array1':JSON.stringify(a), 'array2':JSON.stringify(b)},

However, I also need to add the rest of the form data that has been input by the user. I can get this data with $(this).serialize()but if I try to add that to the datafield, it does not work. How can I add this data to the above line?

但是,我还需要添加用户输入的其余表单数据。我可以获得这些数据,$(this).serialize()但如果我尝试将其添加到该data字段中,则它不起作用。如何将此数据添加到上述行?

Thanks.

谢谢。

SOLUTION

解决方案

What ended up working the way I originally had hoped for (with Nicolas' help):

最终以我最初希望的方式工作(在尼古拉斯的帮助下):

var formData = $(this).serializeArray();
var a_string = JSON.stringify(a);
formData.push({name: 'array1', value: a_string});
var b_string = JSON.stringify(b);
formData.push({name: 'array2', value: b_string});

$.ajax({
    url: "submitOrder.php",
    data: formData,
    type: 'post',
    success: function(data) {
        alert(data);
    }
});            

采纳答案by Nicolás Torres

The data should be encapsuled this way

数据应该这样封装

data: {'first_array':JSON.stringify(array1),'second_array':JSON.stringify(array2)}

Then in PHP:

然后在 PHP 中:

$array1 = json_decode($_POST['first_array']);
$array2 = json_decode($_POST['second_array']);

You can add the rest of the inputs as well.

您也可以添加其余的输入。

data: {'first_array':JSON.stringify(array1),'second_array':JSON.stringify(array2),'input1':$(input[name="input1"]).val()}

Just repeat with all the inputs you want to send.

只需重复您要发送的所有输入即可。

'input1':$(input[name="input1"]).val(),'input2':$(input[name="input2"]).val(),... etc