[英]How can I sent an array with $.ajax - dataType: json?
Im currently trying to sent a javascript array to my .php file handler and save it to my database. 我目前正在尝试将javascript数组发送到我的.php文件处理程序并将其保存到我的数据库中。
The request is successful however it seems my array doesn't get POSTED / saved correctly. 请求成功,但是似乎我的阵列未正确发布/保存。 In my POST request source it just turns up as: round_items%5B%5D=1
在我的POST请求源中,它只是显示为: round_items%5B%5D=1
What am I missing? 我想念什么?
id = 5;
var roundChallenges = new Array("item1", "item2", "etc");
//Save the data
var url = path.php;
var request = $.ajax({
type: "POST",
url: url,
dataType: 'json',
data: { uid: id, round_items: roundChallenges },
success: function(data)
{....
round_items%5B%5D=1
is correct. round_items%5B%5D=1
是正确的。 That is what it should be sending. 那就是它应该发送的。 That decodes to round_items[]=1
, which is how you make arrays in query strings. round_items[]=1
解码为round_items[]=1
,这就是在查询字符串中创建数组的方式。
When you pass an object to $.ajax
, jQuery converts it to a query string, a standard transport format. 当您将对象传递给$.ajax
,jQuery会将其转换为查询字符串(一种标准的传输格式)。
In PHP, you don't need json_decode
or anything. 在PHP中,您不需要json_decode
或其他任何东西。 It will parse it into $_POST
for you. 它将为您解析为$_POST
。 $_POST['round_items']
will be an array, and $_POST['uid']
will be your id
. $_POST['round_items']
将是一个数组,而$_POST['uid']
将是您的id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.