如何使用 Jquery AJAX 帖子传递多维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11747373/
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
How to pass multi-dimensional array with Jquery AJAX post?
提问by user1337603
I've been using Serialize() to pass checkbox form data with Post() for a basket that can hold multiple items of the same category.
我一直在使用 Serialize() 将复选框表单数据与 Post() 传递给一个篮子,该篮子可以容纳多个相同类别的项目。
When I post them using the submit button it works fine with multiple values being passed and displayed under one category.
当我使用提交按钮发布它们时,它可以很好地传递多个值并显示在一个类别下。
However when I used Jquery serialize() it will only show one item per category and only two categories in total. This is an array issue but I cannot work it out.
但是,当我使用 Jquery serialize() 时,它只会显示每个类别一个项目,总共只显示两个类别。这是一个数组问题,但我无法解决。
Is there an alternative JQuery function i should be using to pass a multi-dimensional array?
我应该使用替代的 JQuery 函数来传递多维数组吗?
回答by Branden S. Smith
Jquery will take multi dimensional arrays directly, no need to serialize.
jquery 会直接取多维数组,不需要序列化。
var data = {
foo: 123,
bar: 456,
rows: [
{
column1 : 'hello',
column2 : 'hola',
column3 : 'bonjour',.
},
{
column1 : 'goodbye',
column2 : 'hasta luego',
column3 : 'au revtheitroad',
},
],
test1:{
test2: {
test3: 'baz'
}
}
};
_Post Data in your PHP file would look like this
_在您的 PHP 文件中发布数据如下所示
Array
(
[foo] => 123
[bar] => 456
[rows] => Array
(
[0] => Array
(
[column1] => hello
[column2] => hola
[column3] => bonjour
)
[1] => Array
(
[column1] => goodbye
[column2] => hasta luego
[column3] => au revtheitroad
)
)
[test1] => Array
(
[test2] => Array
(
[test3] => baz
)
)
)
Once you define your data multidimensional array, your Ajax could be as simple as
一旦你定义了你的数据多维数组,你的 Ajax 就可以像这样简单
$.ajax({
type: 'post',
cache: false,
url: './ajax.php',
data: data
});
If your post array may have fields that you don't know about, you can access your Post array in your php file easily with
如果你的 post 数组可能有你不知道的字段,你可以很容易地在你的 php 文件中访问你的 Post 数组
$data = file_get_contents('php://input');
$data = json_decode($data, true);
回答by Maddy
I did not find any good solution, so i solved this using JSON.stringify();here is my code
我没有找到任何好的解决方案,所以我使用JSON.stringify();解决了这个问题。这是我的代码
Client side :
客户端 :
var data = {a:{'foo':'bar'},b:{'this':'that'}};
$.ajax({ url : '/',
type : 'POST',
data : {'data':JSON.stringify(data)},
success : function(){ }
});
Server Side:
服务器端:
$data = json_decode($_POST['data']);
print_r($data);
// Result:
// Array( "a" => Array("foo"=> "bar"), "b" => Array("that" => "this"))
回答by chris
$.post(url, {"myarray":arrayData}, function(data){/*stuff*/}, 'json');
server side you would access it for example with php
服务器端,您可以使用例如 php 访问它
$myArray = $_POST['myarray'][0];
foreach($myArray as $item)
{
/*logic here for picking apart your array*/
}
回答by Aaron
From the jQuery docs:
来自 jQuery 文档:
For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked.
对于要包含在序列化字符串中的表单元素的值,该元素必须具有 name 属性。来自复选框和单选按钮(“radio”或“checkbox”类型的输入)的值仅在被选中时才包含在内。
Check your code for that first. Hard to help further without seeing your code.
首先检查您的代码。很难在没有看到您的代码的情况下进一步提供帮助。
回答by hugo
Here is my metacode fragment, which works fine for me ...
这是我的元代码片段,对我来说很好用......
var f={};
f.text = "SOME DATA";
f.any_other_field = some_other_value;
f.items = [];
$("#droppable .or").each(function(ee){
var tmp={};
tmp.id = $(this).data("cid");
tmp.name = $(this).find(".ornazev").text();
tmp.price = $(this).data("price");
tmp.currency = $(this).data("currency");
tmp.ks = 1;
f.items.push(tmp);
});
$.ajax({
type: "POST",
url: urlsave,
data: {"f":f},
dataType: "text",
.....