用fullcalendar从razor post获取json的错误请求

问题描述 投票:0回答:1

我已经实施了完整的日历。它确实按照预期显示,但是获取数据是错误的

.cshtml

    <div id='calendar'></div>

.cshtml.cs

    public class IndexModel : PageModel
    {
        public JsonResult OnPost(DateTime start, DateTime end)
        {
            return new JsonResult(new
            {
                url = "something",
                title = "something else",
                start = ConvertToUnixTimestamp(DateTime.Now).ToString(),
                end = ConvertToUnixTimestamp(DateTime.Now.AddDays(2)).ToString(),
                allDay = false,
                backgroundColor = "red",
                textColor = "green"
            });
        }
    }

JS

            document.addEventListener('DOMContentLoaded', function () {
                var calendarEl = document.getElementById('calendar');

                var calendar = new FullCalendar.Calendar(calendarEl, {
                    events: {
                        url: '/Overview/Employee/Index',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("XSRF-TOKEN",
                                $('input:hidden[name="__RequestVerificationToken"]').val());
                        },
                        method: 'POST'
                    },
                    plugins: ['dayGrid']
                });

                calendar.render();
            });

当我加载页面时,我看到发生请求,但它返回400,错误的请求。知道为什么吗?

请求:

enter image description here

asp.net-core razor fullcalendar fullcalendar-4
1个回答
0
投票

我尝试了没有400错误的代码(在表单中使用@Html.AntiForgeryToken()),但事件未成功添加到日历中。当我返回List Model时,它会工作,尝试使用下面的代码添加事件:

public class EventModel
{
    public int id { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string title { get; set; }
    public bool allDay { get; set; }
    public string url { get; set; }
    public string color { get; set; }
    public string textColor { get; set; }
}

public class IndexModel : PageModel
{
    public JsonResult OnPost(DateTime start, DateTime end)
    {
        IEnumerable<EventModel> events = new List<EventModel>()
        {
            new EventModel()
            {
                url = "something",
                title = "something else",
                start = (DateTime.Now).ToString(),
                end = (DateTime.Now.AddDays(2)).ToString(),
                allDay = false,
                color = "red",
                textColor = "green"
            }

        };
        return new JsonResult(events);
    }
}

结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.