2015-05-07 33 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