【发布时间】:2014-02-08 13:43:48
【问题描述】:
我创建了一个数组并尝试通过 ajax 函数将它与其他变量一起传递给 PHP 文件。其他变量发送正常,但锻炼数组不发送。
workout = [];
$('.workout > section').each(function (i) {
var self = $(this),
exerciseid = self.attr('id'),
id = i;
workout[id] = []; // key: exercise id
self.find('.exercise > li').each(function(i) {
var self = $(this);
if(!self.hasClass('newitem')) {
workout[id][i] = []; // key: set
workout[id][i]['exerciseid'] = exerciseid;
self.find('input').each(function() {
var self = $(this),
value = self.val(),
role = self.attr('data-type');
workout[id][i][role] = value;
});
};
});
});
Fiddle of the created array。我相信问题就在这里。
$.ajax({
type: "post",
url: "/lib/track.php",
data: {
'token' : token,
'date' : date,
'diet' : diet,
'workout' : workout,
}
})
我错过了什么吗?这是输出的 $_POST 数组的当前状态:
Array
(
[token] => 12E7AS13FEA52B9AEF4074CC8A5E8841C5AA087VA
[date] => 2014-01-13
[diet] => Array
(
[0] => Array
(
[meal] => 2
[foodid] => 4043
[unit] => 3
[altgrams] =>
[altname] =>
[value] => 1
)
)
)
【问题讨论】:
-
你检查你的 JS 函数(每个)被调用了吗?你可以使用 console.log("a log") 来调试它。
-
是的。如果您看到小提琴的控制台,则表示正在创建数组。我很难过为什么它没有通过 ajax 传递。
-
是的,数组没问题,也许你应该在AJAX请求之前调试它。
-
你试过
workout[id][i] = {}(而不是... = [])吗?通常,当数组被序列化时,仅包含实际元素,即具有数字索引的属性,而不是具有非数字字符串键的属性。使用对象 ({}) 应该可以正确序列化。
标签: javascript jquery ajax arrays object