2015-05-07 99 views
0

答案:错误不在同步或异步调用中,而是在使用由fullcalendar设置的函数中。 事件:功能(开始,结束,时区,回调){...}JS,fullcalendar,“events”参数没有得到执行函数的结果

我能看到的信息转换如何管理AJAX(sjax ...)的结果,但日历没有得到信息。

看起来这行“events:getJSON”不起作用。但功能是呼吁

现在,我有一个新的问题,函数getJSON从webmethod获取JSON,但日历不显示它们。

我试图用评论代码也得到结果,但它不起作用。

这里有两种方法栌的JS:

function getJSON() { 
    start = $('#calendar').fullCalendar('getDate').format(); 
    duration = $('#calendar').fullCalendar('getCalendar').moment().format(); 
    alert(start); 
    var retour; 
    $.ajax({ 
     async: false, 
     type: 'POST', 
     url: "ConsultationPlanning.aspx/getPlanning", 
     data: '{"start": "' + start + '", "end": "' + duration + '"}', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      retour = JSON.parse(msg.d); 
      retour = msg.d; 
     }, 
     error: function() { alert("erreur");} 
    }); 
    alert(retour); 
    return retour; 
}; 

$('#calendar').fullCalendar({ 
    header: { 
     left: 'prev,next today', 
     center: 'title', 
     right: 'month,agendaWeek,agendaDay' 
    }, 
    hiddenDays: [0], 
    defaultView: 'agendaWeek', 
    editable: false, 
    allDaySlot: false, 
    selectable: false, 
    events: getJSON, 
     /*function (start, end, callback) { 
     start = $('#calendar').fullCalendar('getDate').format(); 
     duration = $('#calendar').fullCalendar('getCalendar').moment().format(); 
     $.ajax({ 
      type: 'POST', 
      url: "ConsultationPlanning.aspx/getPlanning", 
      data: '{"start": "' + start + '", "end": "' + end + '"}', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       var rdv = []; 
       alert(msg.d); 
       //var events =JSON.parse(msg.d); 
       $(msg.d).each(function() { 
        rdv.push({ 
         id: $(this).attr('id'), 
         title: $(this).attr('title'), 
         start: $(this).attr('start'), 
        }) 
       }) 
       alert("first date : " + rdv[0].start); 
       //"first date: 2015-05-06T14:33:00" is what i get 
       callback(rdv); 
       //the callback generate an error message. it says, it's waiting for a function 

      } 
     }) 
    } 
}); 

我张贴了类似的问题,前几天,但它与其他实现:

fullcalendar doesn't show events

+0

它会工作,请参阅我的答案在这个问题上 http://stackoverflow.com/questions/30092190/populating-events-in-full-calender-javascript -from-the-database/30092608#30092608 – Sherlock

+0

将它称为函数events:getJSON() – Sherlock

+0

当我用圆括号调用函数时,我无法使用“$('#calendar')。fullCalendar('getDate') 。格式();”函数getJSON – Olivier

回答

0

$.ajax触发的异步调用;您的retour变量的值将设置为您的浏览器收到对其请求的响应之后所期望的值。

这就是为什么fullcalendar的events是带一个callback说法:

function getJSON(start, end, callback) { 
    ... 
    $.ajax({ 
     ... 
     success : function(msg) { 
      retour = JSON.parse(msg.d); 
      callback(retour); 
     } 
    }); 
    // no need to return anything here 
} 

见fullcalendar V1的文档*这里:events function


注:如果您使用的fullcalendar v2或更高,您应该将您的功能签名更改为:

function getJSON(start, end, timezone, callback) { 
    ... 

看到V2 DOC *这里:events function

+0

非常感谢你,我使用的是fullcalendar v2,我想我可以在没有时区的情况下运行该函数。 而我只是添加参数而不使用它,它的工作原理。 如果可以,我点击箭头说你的答案在我的情况下是有用的。 此外,我取消注释我的代码并删除我的函数getJSON – Olivier