FullCalendar:如何通过ajax响应在fullcalendar中显示事件

时间:2016-05-27 10:41:44

标签: javascript ajax javascript-events fullcalendar

我正在全日历工作。现在我需要通过ajax调用显示数据库中的所有事件。直到现在,我已经通过json从数据库中获取了事件。 我的json输出在var_dump:

之后如下
array (size=3)
  0 => 
    array (size=7)
      'title' => string 'hi' (length=2)
      'id' => int 1
      'start' => string '2016-05-30 00:00:00.000' (length=23)
      'rendering' => string 'background' (length=10)
      'backgroundColor' => string '#ff9999' (length=7)
      'className' => string 'event-full' (length=10)
      'textColor' => string 'white' (length=5)
  1 => 
    array (size=7)
      'title' => string 'vibrator' (length=8)
      'id' => int 3
      'start' => string '2016-05-26 00:00:00.000' (length=23)
      'rendering' => string 'background' (length=10)
      'backgroundColor' => string '#ff9999' (length=7)
      'className' => string 'event-full' (length=10)
      'textColor' => string 'white' (length=5)
  2 => 
    array (size=7)
      'title' => string 'vibrator' (length=8)
      'id' => int 8
      'start' => string '2016-05-26 00:00:00.000' (length=23)
      'rendering' => string 'background' (length=10)
      'backgroundColor' => string '#ff9999' (length=7)
      'className' => string 'event-full' (length=10)
      'textColor' => string 'white' (length=5)

我的脚本通过我的ajax如下: -

 events: function(start, end, timezone, callback)
{    
$.ajax({
        url: "calendar/show_events",
        type: 'POST', // Send post data
        data: 'type=fetch_events',
        async: true,

        success: function(s){

             dynamic_events =s;
                       alert(dynamic_events);
             // $('#calendar').fullCalendar('addEventSource', JSON.parse(dynamic_events));

              var dynamic_events = [];
                $(doc).find('event').each(function() {
                    events.push({
                        title: $(this).attr('title'),
                        start: $(this).attr('start') // will be parsed
                    });
                });
                callback(events);

              }
    });    
}

我的代码中有什么错误..我现在想学习。 任何人都可以帮助我,节省我的一天。 我需要在FullCalendar中显示所有事件。在此先感谢

1 个答案:

答案 0 :(得分:-1)

将您的ajax代码更改为

$.ajax({
    url: "calendar/show_events",
    type: 'POST', // Send post data
    data: 'type=fetch_events',
    async: true,

    success: function(s){

          var dynamic_events = [];
            $(s).find('event').each(function() {
                dynamic_events.push({
                    title: $(this).attr('title'),
                    start: $(this).attr('start') // will be parsed
                });
            });
            callback(dynamic_events);

          }
});