FullCalendar Json事件无法通过函数加载

时间:2014-02-05 16:33:03

标签: jquery ajax json vb.net fullcalendar

我在vb.net web项目中使用fullcalendar控件。如果我将事件的硬编码:完整日历上的属性转换为正确加载的字符串,但我无法通过POST函数加载数据。

这是使用页面

中的字符串的硬编码值
events: [<%=eventstring %>]

这是我在后面的代码中硬编码的测试值。

eventstring = "{ title: 'Testing - This is a test', start:'2014-02-05T19:10:00-04:00' , end: '2014-02-05T19:10:00-03:00', allDay : false   }"

正确显示日历上的活动。如果我尝试以下操作,则无法在日历上加载活动。

events: function (start, end, callback) {
    $.ajax({
        type: "POST",
        url: "calendar.aspx/LoadCalendarEvents",
        data: '{startDate: "' + $.fullCalendar.formatDate(start, 'M/d/yyyy') + '",' + 'endDate: "' + $.fullCalendar.formatDate(end, 'M/d/yyyy') + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",

    });
},

这是代码隐藏中的功能。它返回完全相同的字符串。

<System.Web.Services.WebMethod()> _
Public Shared Function LoadCalendarEvents(ByVal startDate As String, ByVal endDate As String) As String
    Dim eventstring As String = ""

    eventstring = "{ title: 'Testing - This is a test', start:'2014-02-05T19:10:00-04:00' , end: '2014-02-05T19:11:00-03:00', allDay : false   }"

    Return eventstring
End Function

我在LoadCalendarEvents函数中放置了一个断点,并且可以看到当日历上的日期发生变化并且正在对字符串进行撤销时它会被调用。

我错过了什么?使用POST功能时,为什么事件不会显示在日历上?

2 个答案:

答案 0 :(得分:0)

查看fullcalendar文档: http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

来自文档:

$('#calendar').fullCalendar({

    eventSources: [

        // your event source
        {
            url: '/myfeed.php',
            type: 'POST',
            data: {
                custom_param1: 'something',
                custom_param2: 'somethingelse'
            },
            error: function() {
                alert('there was an error while fetching events!');
            },
            color: 'yellow',   // a non-ajax option
            textColor: 'black' // a non-ajax option
        }

        // any other sources...

    ]

});

答案 1 :(得分:0)

通过让webmethod返回数组而不是json字符串,我能够通过fullcalendar成功加载事件。

这就是我在javascript中设置eventsources函数的方法。我从成功函数中删除了其他字段以及其他一些函数来加载来自不同来源的事件以缩短答案。

eventSources: [
                // Load Standard Events For Employee
                function (start, end, callback) {
                    $.ajax({
                        type: "POST",
                        url: "/calendar.aspx/LoadCalendarEvents",
                        data: '{startDate: "' + $.fullCalendar.formatDate(start, 'M/d/yyyy') + '",' + 'endDate: "' + $.fullCalendar.formatDate(end, 'M/d/yyyy') + '  "   , employeeID: "' + $('#lstEmployeesMaster').val() + '"       }',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",

                        success: function (eventstring) {
                            var buildingEvents = $.map(eventstring.d, function (item) {
                                return {
                                    id: item.EventID,
                                    title: item.Title,
                                    start: item.StartDate,
                                    end: item.EndDate,
                                    allDay: false,
                                    //...(More Fields)
                                };
                            });

                            callback(buildingEvents);
                        },
                        error: function (data) {
                            //alert(data);
                        }
                    });
                },
               //...(Additional Functions for other events)
             ],

网络方法基本上如下所示。我刚刚删除了一些与问题无关的其他逻辑。您可以跳过while循环并只使用(您的类).ToArray()的列表,但我需要一段时间才能从此代码中删除其他功能。

<System.Web.Services.WebMethod()> _
Public Shared Function LoadCalendarEvents(ByVal startDate As String, ByVal endDate As String, ByVal employeeID As String) As Array
    Dim events As DataSet

    If employeeID > 0 Then ' Individual Employee
        events = dbEvents.ListByEmployeeForNewCalendar(employeeID, startDate, endDate)
    Else ' Office
        events = dbEvents.ListByOfficeForNewCalendar(-1 * employeeID, startDate, endDate)
    End If

    Dim eventList As New ArrayList()

    Dim i As Int32 = 0
    While i < events.Tables(0).Rows.Count ' Load the details for each event in the range
        Dim startonDate As DateTime = CDate(events.Tables(0).Rows(i).Item(14))
        Dim endonDate As DateTime = CDate(events.Tables(0).Rows(i).Item(15))

        Dim eventRecord As New CalendarEventDetail ' This is a simple class with properties for the event detail
        eventRecord.StartDate = Format(startonDate, "yyyy-MM-dd HH:mm:ss")
        eventRecord.EndDate = Format(endonDate, "yyyy-MM-dd HH:mm:ss")
        eventRecord.EventID = events.Tables(0).Rows(i).Item(0).ToString()
        ' Additional stuff removed

        eventList.Add(eventRecord)

        i += 1
    End While

    Return eventList.ToArray
End Function