2012-07-08 48 views
0

我試圖創建一個JQuery FullCalendar使用WebService方法作爲數據源。完整日曆ajax與.NET WebService的數據源

我的方法是:

[WebMethod] 
public EventData[] ListEvents(int start, int end) 
{ 
} 

我的JavaScript:

$('#calendar').fullCalendar({ 
    events: 'MyWebService.asmx/ListEvents' 
} 

所請求的網址是:

http://localhost:49354/MyService.asmx/ListEvents?start=1338073200&end=1341702000&_=1341766605921 

哪個不解決,我怎麼可以更新這個JQuery調用我的WebMethod是否正確?

+1

這可能會幫助:HTTP:// growingtech。 blogspot.com/2012/02/full-calendar-with-json-data-source.html。我決定使用一個標準的ASPX頁面,並使用JavaScriptSerializer將事件寫入Response ......比映射上面鏈接中的所有內容似乎更容易。 – 2012-07-08 18:18:17

回答

0

我找到了解決辦法:

$(document).ready(function() { 
     $('#Calendar').fullCalendar({ 
      events: function (start, end, callback) { 

      $.ajax({ 
       type: "POST", 
       url: 'Webservice.asmx/ListEvents', 
       cache: false, 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data) { 
       var events = []; 
       $(data.d).each(function() { 
        events.push({ 
        title: this.Title, 
        start: this.Start, 
        end: this.End 
        }); 
       }); 
       callback(events); 
       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
       alert('There was an error'); 
       } 
      }); 
      } 
     }); 
     }); 

在這個例子中,因爲EPOC我的數據我的web服務返回倍的秒數:

[WebMethod] 
    public CalendarEvent[] ListEvents() 
    { 
     DateTime epoc = new DateTime(1970, 1, 1); 
     return new CalendarEvent[] 
     {   
     new CalendarEvent { Title = "Event 1", Start = new DateTime(2012,7,9,16,0,0).Subtract(epoc).TotalSeconds, End = new DateTime(2012,7,9,17,0,0).Subtract(epoc).TotalSeconds}, 
     new CalendarEvent { Title = "Event 2", Start = new DateTime(2012,7,12,12,0,0).Subtract(epoc).TotalSeconds, End = new DateTime(2012,7,12,13,0,0).Subtract(epoc).TotalSeconds} 
     };  
    }