Add custom headers to Ajax when loading data

Add custom headers to Ajax when loading data

alesnavalesnav Posts: 19Questions: 5Answers: 1

Hello,

I need to add custom headers to Ajax requests when loading data for DataTables, but I don't know if it is possible directly from DataTables.

It is possible to add data to the Ajax petition, but I don't see any option to add custom headers. This is how we included the data:

'ajax': { 'url': url, 'type': 'GET', 'data': function ( data ) { data.token = token; } }

How can we add custom headers to the Ajax call in DataTables?

Using jQuery, it would be:

$.ajax({ type: 'GET', url: url, data: '', beforeSend: function (request) { request.setRequestHeader("token", token); } });

Thanks!

Replies

  • alesnavalesnav Posts: 19Questions: 5Answers: 1
    edited March 2015

    Ah, nice!

    You can add 'beforeSend' the same way in DataTables!!! :)

    Allan, could you add this to docs?

            'ajax': {
                'url': url,
                'type': 'GET',
                'beforeSend': function (request) {
                    request.setRequestHeader("token", token);
                }
            }
    
This discussion has been closed.