通过Ajax传递数组

时间:2013-04-18 15:15:13

标签: ajax arrays multidimensional-array

我正在尝试通过ajax调用将数组数组传递给我的php脚本。

var myArray=$('input#thisIsAnArray').val();

var dataString='passedArray='+myArray;

$.ajax({
     type: "POST",
     dataType: "json",
     url: "includes/myPhp.php",
     data: dataString,
     success:function(){


               }

});

然后在我的Php.php中:

print_r($_POST['passedArray'][0][0]);

我收到这条迟钝的消息:

 Fatal error: Cannot use string offset as an array

这没有任何意义,因为我使用整数来访问数组而不是字符串。

JSON对象结构是:

0>
 0>
  admin_id: 1
  status: 1
  date: 1366300373
  outcome_id: 1
  rank: 1
 1>
  admin_id: 2
  status: 2
  date: 1366300373
  outcome_id: 5
  rank: 6

提前致谢。

1 个答案:

答案 0 :(得分:0)

请注意,dataType中的jQuery.ajax()只是HTTP调用中的预期响应。所以你想要做的是指定contentType

更多信息here

另外,我真的不知道PHP如何解析您发送的数据,因此请确保=在这里工作。

编辑:请您解释一下这些数字0>1>是什么意思?因为据我所知JSON看起来并不像那样。

JSON中的数组以这样的方式编写:

[
    "first value": 1,
    "second value": "a string",
    "and so on": 500,
    "note that the last key does not need a comma",
    "and a key does not need to have a value assigned to it"
]

以这种方式编写数组数组:

[
    ["key": "value in an array which itself is contained in an array"],
    ["second array here"],
    ["and last array, without comma"]
]