Ajax 返回数组在 PHP 中显示 [object Object],[object Object] [英] Ajax return array shows [object Object],[object Object] in PHP
问题描述
在 Jquery 中,我创建了两个数组,一个嵌入到另一个数组中,就像这样......
arrayOne = [{name:'a',value:1}, {name:'b',value:2}]var arrayTwo = [{name:'foo',value:'blah'},{name:'arrayOne',value:arrayOne}];
然后我将它放在 Ajax 中,并通过 PHP 在另一端提取变量.一个print_r($arrayTwo)的结果如下...
Array([foo] => blah [arrayOne] => [object Object],[object Object])
我看不到提取arrayOne的内容的方法,很遗憾,因为我真的需要它们!谁能告诉我我在 Jquery 中做错了什么,或者我需要在 PHP 中做什么才能使嵌入式数组可访问.
一如既往的感谢
<小时>编辑以添加我的 Ajax 代码....
$.ajax({类型:POST",url:'actions.php',数据:数组二,数据类型:'json',缓存:假,成功:功能(数据){}
})
问题在于 jQuery 的 $.ajax
(或者更确切地说是 $.param
)方法处理数组以特殊的方式处理对象.jQuery 将使用 name
作为参数名称,使用 value
的 string 表示作为值:
<代码>>$.param([{name: 'foo', value: 42}, {name: 'bar', value: 21}])foo=42&bar=21"
但是 arrayOne
的字符串表示是你在服务器上看到的无用的东西:
<代码>>[{name:'a',value:1}, {name:'b',value:2}].toString()[对象对象],[对象对象]"
文档 实际上指出了传递数组/对象时的注意事项:
<块引用>如果传入的对象是Array,则必须是.serializeArray()
返回格式的对象数组
注意:由于某些框架解析序列化数组的能力有限,因此在传递包含嵌套在另一个数组中的对象或数组的 obj
参数时,开发人员应谨慎行事.
注意:因为没有普遍认可的参数字符串规范,所以不可能使用这种方法以一种在支持此类输入的所有语言中都能理想地工作的方式对复杂的数据结构进行编码.使用 JSON 格式替代编码复杂数据.
<小时>
由于您有一个复杂的数据结构,您可能应该使用 JSON 来编码您的数据:
data: {data: JSON.stringify(arrayTwo)},
在服务器上,您只需使用
对其进行解码$data = json_decode($_POST['data'], true);
$data
将具有与 arrayTwo
完全相同的结构.
但是如果你真的想拥有名称为foo
和arrayOne
的参数,那么你只需要序列化arrayOne
的值:
数据:[{name:'foo',value:'blah'},{名称:'arrayOne',值:JSON.stringify(arrayOne)}],
在 PHP 中:
$arrayOne = json_decode($_POST['arrayOne'], true);
Within Jquery I am creating two arrays, one embedded in the other, like so....
arrayOne = [{name:'a',value:1}, {name:'b',value:2}]
var arrayTwo = [{name:'foo',value:'blah'},{name:'arrayOne',value:arrayOne}];
I am then putting this though Ajax and extracting the variable via PHP on the other side. The results of a print_r($arrayTwo) are as follows...
Array([foo] => blah [arrayOne] => [object Object],[object Object])
I can see no way of extracting the contents of arrayOne, which is a pity because I really need them! Can anyone tell me what I am doing wrong in Jquery or what I need to do in PHP to make the embedded array accessible.
Many thanks as always
Edit to add my Ajax code....
$.ajax({
type: "POST",
url:'actions.php',
data:arrayTwo,
datatype:'json',
cache: false,
success: function(data){
}
})
The issue is that jQuery's $.ajax
(or rather $.param
) method treats an array of objects in a special way. jQuery will use name
as the parameter name and the string representation of value
as value:
> $.param([{name: 'foo', value: 42}, {name: 'bar', value: 21}])
"foo=42&bar=21"
But the string representation of arrayOne
is the useless stuff you are seeing on the server:
> [{name:'a',value:1}, {name:'b',value:2}].toString()
"[object Object],[object Object]"
The documentation actually points out the caveats when passing an array / object:
If the object passed is in an Array, it must be an array of objects in the format returned by
.serializeArray()
[ { name: "first", value: "Rick" }, { name: "last", value: "Astley" }, { name: "job", value: "Rock Star" } ]
Note: Because some frameworks have limited ability to parse serialized arrays, developers should exercise caution when passing an
obj
argument that contains objects or arrays nested within another array.Note: Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Use JSON format as an alternative for encoding complex data instead.
Since you have a complex data structure, you should probably use JSON to encode your data:
data: {data: JSON.stringify(arrayTwo)},
and on the server you simply decode it with
$data = json_decode($_POST['data'], true);
$data
will have the exact same structure as arrayTwo
.
But in case you want to actually have parameters with names foo
and arrayOne
, then you only need to serialize the the value of arrayOne
:
data: [
{name:'foo',value:'blah'},
{name:'arrayOne',value: JSON.stringify(arrayOne)}
],
and in PHP:
$arrayOne = json_decode($_POST['arrayOne'], true);
这篇关于Ajax 返回数组在 PHP 中显示 [object Object],[object Object]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!