【问题标题】:FullCalendar: how to display events in fullcalendar through ajax responseFullCalendar:如何通过ajax响应在fullcalendar中显示事件
【发布时间】:2016-09-25 16:02:39
【问题描述】:

我正在按日历工作。现在我需要通过 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 中显示所有事件。

【问题讨论】:

  • 你得到什么错误?
  • @Eyal Abir, Uncaught ReferenceError: doc is not defined
  • 直到我的警报(动态事件);我收到了回复,但我不知道如何在 fullcalendar 中制作事件
  • 您误用了成功函数中的变量。 doc 确实没有定义。您可能打算检查 s 变量。
  • 好的,如果是这样,我应该如何检查's'变量。不知何故,我想以这种方式显示完整日历中的事件..

标签: javascript jquery ajax fullcalendar jquery-events


【解决方案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);

          }
});    

【讨论】:

  • 但是我得到了这样的错误,当我这样做时..Uncaught Error: Syntax error, unrecognized expression: [{"title":"hi","id":1,"start ":"2016-05-30 00:00:00.000","re​​ndering":"background","backgroundColor":"#ff9999","className":"event-full","textColor":"white"} ,{"title":"vibrator","id":3,"start":"2016-05-26 00:00:00.000","re​​ndering":"background","backgroundColor":"#ff9999", "className":"event-full","textColor":"white"},{"title":"vibrator","id":8,"start":"2016-05-26 00:00:00.000" ,"rendering":"background","backgroundColor":"#ff9999","className":"event-full","textColor":"white"}}]
  • 什么时候发生?无论如何,这个字符串有 2 个问题,在结尾之前有一个额外的 } 并且由于某种原因有一个特殊字符可能会让你遇到问题。我复制粘贴到fiddle看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 2021-11-27
相关资源
最近更新 更多