0

我目前正在尝试将一个 javascript 数组发送到我的 .php 文件处理程序并将其保存到我的数据库中。

请求成功,但似乎我的数组没有正确发布/保存。在我的 POST 请求源中,它只是显示为:round_items%5B%5D=1

我错过了什么?

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)
    {....
4

1 回答 1

1

round_items%5B%5D=1是正确的。这就是它应该发送的内容。这将解码为round_items[]=1,这就是您在查询字符串中创建数组的方式。

当您将对象传递给 时$.ajax,jQuery 会将其转换为查询字符串,这是一种标准传输格式。

在 PHP 中,您不需要json_decode任何东西。它会$_POST为你解析它。 $_POST['round_items']将是一个数组,并且$_POST['uid']将是您的id.

于 2013-09-30T19:37:20.263 回答