我有以下jQuery代码:
function saveSchedule()
{
var events = [];
$('ul#schedule-list').children().each(function() {
events.push($(this).attr('value'));
});
jQuery.each(events, function()
{
alert(this);
});
$.post("schedule_service.php?action=save_schedule",
{ events: events, studentid: $('#student').val() },
function(data) {
alert(data);
}, 'json');
}
Run Code Online (Sandbox Code Playgroud)
它从我的页面上的列表中获取所有"值",并将它们放入事件数组中.
然后,在下面我将该数组加上一个studentid传入我的$ .post调用的数据部分.
但是,当我在PHP端收到这个数组时,它似乎是一个奇异的值:
<?php
include_once('JSON.php');
$json = new Services_JSON();
if ($_GET['action'] == 'save_schedule')
{
$event_list = $_POST['events'];
echo $json->encode($event_list);
exit;
}
?>
Run Code Online (Sandbox Code Playgroud)
(注意:我使用的是旧版本的PHP,因此使用的是JSON.php库.)
现在,所有这些返回的都是"14",这是events数组中的最后一个事件.
帖子:
响应:
我如何处理在$ .post中传递数组错误?
这可能看起来很愚蠢,但试试这个:
$.post("schedule_service.php?action=save_schedule",
{ 'events[]': events, studentid: $('#student').val() },
function(data) {
alert(data);
}, 'json');
Run Code Online (Sandbox Code Playgroud)
PHP会events[]自动将密钥名称解析为php中的实际数组...