JQuery isn't parsing JSON response properly

纵饮孤独 提交于 2019-12-06 01:49:36

Your return type from your method GetEvents is string isn't it?

Try returning a List<Event> (or whatever your object is called) and then you don't need to go through the mess of unescaping a JSON string.

$.ajax({
    type: "POST",
    url: "Default.aspx/GetEvents", //return type List<Event>
    data: "{'userID':" + "'B473795D-306A-4718-804B-2813884D5B48'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (doc) {
        //doc.d = [Event,Event,Event] (no processing needed)
        callback(events);
    }
});

In response to your comment, a method with return type of List<> will look like :

[{"title": "sometitle", "start": "yourData"},{"title":"someTitle2", "start":"yourStart2"}]

Translating ASP.NET ASMX webmethod DateTime serialization to JavaScript Date object:

DateFromASPNET = function (sNetDate) {
    if (sNetDate == null) return;
    var r = /\/Date\(([0-9]+)\)\//i
    var matches = sNetDate.match(r);
    if (matches.length == 2) {
        return new Date(parseInt(matches[1]));
    }
    else {
        return sNetDate;
    }
}

The problem is that AJAX is aysnchronous so by passing a function to events option, nothing is returned due to the AJAX call.

Use the built in AJX methods of the plugin by simply passing a ajax options to eventsources option.

From Docs: http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

$('#calendar').fullCalendar({
   eventSources: [

    // your event source
    {
        url: "Default.aspx/GetEvents",
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        data: "{'userID':" + "'B473795D-306A-4718-804B-2813884D5B48'}"
    }

 ]
});

EDIT: can also pass single event source ajax options object to events option per docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!