使用.NET WebService的完整日历ajax DataSource

时间:2012-07-08 17:13:53

标签: jquery web-services fullcalendar

我正在尝试创建一个JQuery FullCalendar以使用WebService方法作为数据源。

我的方法是:

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

我的javascript是:

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

请求的URL是:

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

哪个无法解决,如何更新此JQuery以正确调用我的WebMethod?

1 个答案:

答案 0 :(得分: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');
              }
            });
          }
        });
      });

在此示例中,我的数据我的Web服务返回的时间是自epoc以来的秒数:

[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}
      };      
    }