chrony
chrony

Reputation: 783

How to add custom header to fullcalendar sources

I need to attach a custom header to the ajax request of fullcalendar.

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

});

How do I define that?

Upvotes: 2

Views: 3197

Answers (3)

Adeel Warraich
Adeel Warraich

Reputation: 51

                        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);
                                }
                            });
                        },

Upvotes: 0

chrony
chrony

Reputation: 783

Figured, it was actually as easy as supplying a headers key:

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

Upvotes: 1

Phylogenesis
Phylogenesis

Reputation: 7880

Looking at the documentation, the following is stated:

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.

Following this, the documentation for $.ajax() has the following option:

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)

So the below code should work:

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

Upvotes: 4

Related Questions