fullCalendar events not showing even though correct JSON feed

前端 未结 1 1949
不知归路
不知归路 2021-01-22 06:23

As a bunch of others I have a problem getting my JSON feed events to render in the calendar. The problem is often wrong JSON formating, but this is not the case since I\'ve vali

1条回答
  •  深忆病人
    2021-01-22 06:47

    Try this , you have to have a webmethod in aspx file that fullcalendar can call asynchronously

           $(document).ready(function () {
            $('#fullcal').fullCalendar({
            eventClick: function() {
                alert('a day has been clicked!');
            }, 
                events: function (start, end, callback) {
                $.ajax({
                    type: "POST",    //WebMethods will not allow GET
                    url: "json.aspx/GetEvents",   //url of a webmethod - example below
                    data: "{'userID':'" + $('#<%= hidUserID.ClientID %>').val() + "'}",  //this is what I use to pass who's calendar it is 
                    //completely take out 'data:' line if you don't want to pass to webmethod - Important to also change webmethod to not accept any parameters 
                    contentType: "application/json; charset=utf-8",  
                    dataType: "json",
                    success: function (doc) {
                        var events = [];   //javascript event object created here
                        var obj = $.parseJSON(doc.d);  //.net returns json wrapped in "d"
                        $(obj.event).each(function () { //yours is obj.calevent                          
                                events.push({
                                title: $(this).attr('title'),  //your calevent object has identical parameters 'title', 'start', ect, so this will work
                                start: $(this).attr('start'), // will be parsed into DateTime object    
                                end: $(this).attr('end'),
                                id: $(this).attr('id')
                            });
                        });                     
                        callback(events);
                    }
                });
            }
           })
    

    then this would be in json.aspx

    [WebMethod(EnableSession = true)]
    public static string GetEvents(string userID)
    {
        DataClassesDataContext db = new DataClassesDataContext();
    List eventList = db.calevents.ToList();
    
    // Select events and return datetime as sortable XML Schema style.
    var events = from ev in eventList
                 select new
                 {
                     id = ev.event_id,
                     title = ev.title,
                     info = ev.description,
                     start = ev.event_start.ToString("s"),
                     end = ev.event_end.ToString("s"),
                     user = ev.user_id
                 };
    
    // Serialize to JSON string.
    JavaScriptSerializer jss = new JavaScriptSerializer();
    String json = jss.Serialize(events);
    return json;
    }
    

    0 讨论(0)
提交回复
热议问题