我正在尝试使用jQuery传递这样的数组,但是PHP脚本从未收到该数组,而通过Chrome浏览器的网络跟踪显示,仅传递了 title。
var items = new Array();
items[0] = new Array();
items[0]['code'] = '1234';
items[0]['checked'] = true;
items[1] = new Array();
items[1]['code'] = '4524';
items[1]['checked'] = false;
这是执行AJAX请求的代码:
var sUrl = "<?= base_url(); ?>list/create/";
var serialized = {
title: 'Some Value Here',
items: items
};
$.ajax({
url: sUrl,
type: "POST",
data: serialized,
success: function(data) {
alert(data);
list_id = data;
}
});
问题似乎是jQuery无法序列化它。有没有办法解决? 谢谢!
What happens when you create your items object like this:
var items = [
{
code: '1234'
, checked: true
}
,{
code: '4568'
, checked: false
}
];
I think the problem is you're creating an array with your interior object when you should just be creating a standard object.
Actually you create an array and array cannot be indexed with string. If you must use string as index you should use object instead as provided in Jason's answer or:
items = new Array();
items[0] = new Object();
items[0]['code'] = '1234';
items[0]['checked'] = true;
You're assigning a new Array
to each index in items
, but then you are assigning a new code
and checked
property to that array. This isn't serialized properly because rather than having an object at each position in the array, you have another array with some random properties (code
and checked
) that you've added.
Try assigning an object literal to each position in the array:
var items = [];
items[0] = {
code: '1234',
checked: true
};
items[1] = {
code: '4524',
checked: false
};