jQuery UI DatePicker - 使用ajax调用更新全局事件数组

时间:2013-11-22 08:15:56

标签: php jquery ajax datepicker

我有一个jQuery UI DatePicker,点击时突出显示事件和事件信息。事件设置为静态(硬编码)数组,格式如下:

var events = [ 
    { Title: "Event 1", Date: new Date("11/15/2013") }, 
    { Title: "Event 2", Date: new Date("11/15/2013") }, 
    { Title: "Event 3", Date: new Date("11/25/2013") }, 
    { Title: "Event 4", Date: new Date("11/30/2013") }
];

这很有效,但是事件数组需要在DatePicker'onChangeMonthYear'上更新,所以我每次选择一个新月时都会添加一个ajax调用但是有些麻烦。

问题:如何使用ajax调用返回来更新全局事件数组?

这是我到目前为止所尝试的: PHP

$items = array();
foreach ($events as $event) {
$items[]= array('Title' => $event['SUMMARY'],'Date' => gmdate("m/d/Y",$event['DTSTART']));
}
echo json_encode($items);

JS

$.ajax({ 
type: 'GET',
async : false,
data: {start : firstDay, end : lastDay},
url: "ajax/getCalendarItems.php",
success: function(data){
    events = data;
}
});

结果:

console.log(data)
[{"Title":"New event 1","Date":"12\/20\/2013"},{"Title":"New event 2","Date":"12\/20\/2013"},{"Title":"New event 3","Date":"12\/23\/2013"},{"Title":"New event 4","Date":"12\/30\/2013"}]

TypeError: '[{"Title":"New event 1","Date":"12\/20\/2013"},{"Title":"New event 2","Date":"12\/20\/2013"},{"Title":"New event 3","Date":"12\/23\/2013"},{"Title":"New event 4","Date":"12\/30\/2013"}]' is not a valid argument for 'in' (evaluating 't-1 in e')

1 个答案:

答案 0 :(得分:2)

您是否尝试过设置

        dataType: "json",
    contentType: "application/json; charset=utf-8",

你在ajax调用的参数?它应该自动解码您从PHP服务器传递的json数据。

因为您的'data'参数似乎仍然是JSON。

让我知道! :)