无法从服务器获取全日历插件的事件

Can't get events from server for fullcalendar plugin

我在文件中定义了几个事件,HomeController.cs。我想在 fullcalendar jquery 插件加载时检索这些事件。 这是 HomeController 文件中的方法:

[HttpPost]
    public ActionResult GetAllEvents()
    {
        var eventList = GetEvents();
        var rows = eventList.ToArray();
        return Json(rows, JsonRequestBehavior.AllowGet);
    }

    private static List<Events> GetEvents()
    {
        List<Events> eventList = new List<Events>();
        Events newEvent = new Events
        {
            id = "1",
            title = "Event 1",
            start = DateTime.Now.AddDays(5).ToString("s"),
            end = DateTime.Now.AddDays(7).ToString("s"),
            allDay = true
        };

        eventList.Add(newEvent);
        newEvent = new Events
        {
            id = "1",
            title = "Event 3",
            start = DateTime.Now.AddDays(2).ToString("s"),
            end = DateTime.Now.AddDays(3).ToString("s"),
            allDay = true
        };
        eventList.Add(newEvent);
        return eventList;
    }

Index.cshtml页面上的日历是基本的:

这是jquery文档就绪功能,可以显示日历。日历显示但没有事件。此外,错误警报,获取事件时出错! ,弹出。
这是文档就绪函数:

$(document).ready(function () {
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    $('#calendar').fullCalendar({   
        header:
        {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        titleFormat: {month: 'MMMM'},       
        defaultView: 'month',
        editable: false,    
        events: function (start, end, timezone, callback) {
            $.ajax({
                type: "POST",    
                url: "/HomeController/GetAllEvents/",               
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (doc) {              
                    var events = [];   
                    var obj = $.parseJSON(doc.d);  

                    $(obj.event).each(function () {                      
                        events.push({
                            title: $(this).attr('title'),  
                            start: $(this).attr('start'), 
                            end: $(this).attr('end'),
                            id: $(this).attr('id')
                        });
                    });
                    callback(events);
                },
                error: function () {
                    alert("There was an error fetching events!")
                }
            });
        }
    });
});

我是 jquery 的新手,不确定我是否正确定义了 ajax 函数,尤其是 'url'。另外,我不确定 .parseJSON 是否正确。

如有任何建议,我们将不胜感激。

谢谢

因为我已经将 return 类型定义为 'json',所以我不需要 $.parseJson() 函数。 这是最终的 javascript 有效:

events: function (start, end, timezone, callback) {
$.ajax({
    type: "POST",
    url: '@Url.Action("GetAllEvents", "Home")',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (doc) {
        var events = [];                         
        $(doc).each(function () { 
            events.push({
                title: $(this).attr('title'),  
                start: $(this).attr('start'), 
                end: $(this).attr('end'),
                id: $(this).attr('id')
            });
        });
        callback(events);
        },                                                                    
    error: function () {
        alert("There was an error fetching events!")
    }
});
}

变化包括:

  1. 替换 'url' 值 - 谢谢 Sushil!
  2. 删除函数 $.parseJson()。