如何将自定义 header 添加到全日历源

How to add custom header to fullcalendar sources

我需要将自定义 header 附加到 fullcalendar 的 ajax 请求。

$('#calendar').fullCalendar({
    eventSources: [
        {
            url: '/myfeed.php',
        }    
    ]

});

我该如何定义它?

查看documentation,声明如下:

jQuery $.ajax options

You can also specify any of the jQuery $.ajax options within the same object! This allows you to easily pass additional parameters to your feed script, as well as listen to ajax callbacks.

在此之后,documentation for $.ajax() 有以下选项:

headers (default: {})

Type: PlainObject

An object of additional header key/value pairs to send along with requests using the XMLHttpRequest transport. The header X-Requested-With: XMLHttpRequest is always added, but its default XMLHttpRequest value can be changed here. Values in the headers setting can also be overwritten from within the beforeSend function. (version added: 1.5)

所以下面的代码应该可以工作:

$('#calendar').fullCalendar({
    eventSources: [
        {
            url: '/myfeed.php',
            headers: { myCustomHeader: 'My Custom Value' }
        }    
    ]
});

想通了,实际上就像提供 headers 密钥一样简单:

$('#calendar').fullCalendar({
    eventSources: [
        {
            url: '/myfeed.php',
            headers: {
                'Authorization': 'foo'
            }
        }
    ]
});
                        events: function (info, successCallback, failureCallback) {
                            let start = moment(info.start.valueOf()).format('YYYY-MM-DD');
                            let end = moment(info.end.valueOf()).format('YYYY-MM-DD');
                            $.ajax({
                                url: 'feed.php',
                                type: 'GET',
                                data:{
                                    'start':start,
                                    'end':end,
                                    'id': '12'
                                },
                                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
                    'accept': 'application/json',
                    'X-Requested-With': 'XMLHttpRequest',
                    'X-CSRF-Token': $('meta[name=csrf-token]').attr("content"), //Here, I am using the CSRF token
                };,
                                success: function (response) {
                                    successCallback(response);
                                }
                            });
                        },