通过Ajax传递数组

时间:2022-01-07 03:07:30

I'm trying to pass an array of arrays via an ajax call to my php script.

我试图通过ajax调用传递一个数组数组到我的PHP脚本。

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

var dataString='passedArray='+myArray;

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


               }

});

Then in my Php.php:

然后在我的Php.php中:

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

I get this retarded message:

我得到这个迟钝的消息:

 Fatal error: Cannot use string offset as an array

Which makes no sense because I am using integers to access the array not a string.

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

The JSON object structure is:

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

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

Remember well that dataType in jQuery.ajax() is only the expected response from the HTTP call. So what you want to do here is to specify the contentType.

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

More information here.

更多信息在这里。

Also, I do not really know how PHP parses the data you send, so make sure the = works here.

另外,我真的不知道PHP如何解析你发送的数据,所以确保=在这里工作。

EDIT: would you please explain to me what those numbers 0>, and 1> mean? Because as far as I know JSON does not look like that at all.

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

An array in JSON is written that way:

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"
]

An array of arrays is written that way:

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

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

#1


0  

Remember well that dataType in jQuery.ajax() is only the expected response from the HTTP call. So what you want to do here is to specify the contentType.

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

More information here.

更多信息在这里。

Also, I do not really know how PHP parses the data you send, so make sure the = works here.

另外,我真的不知道PHP如何解析你发送的数据,所以确保=在这里工作。

EDIT: would you please explain to me what those numbers 0>, and 1> mean? Because as far as I know JSON does not look like that at all.

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

An array in JSON is written that way:

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"
]

An array of arrays is written that way:

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

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