Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interceptor for Authorization headers using Kendo UI datasource

I am using webapi and restrict web api's to authenticate by token, so to populate datasource I use request headers in DataSource.

var abcDatasource = new kendo.data.DataSource({
    transport: {
        read: {
            url: '/api/exampledata',
            dataType: 'json',
            headers: { 'Authorization': 'Bearer ' + accesstoken }
        },
    },
    pageSize: 5, 

});

the below line of code need to repeat at all datasource

headers: { 'Authorization': 'Bearer ' + accesstoken }

Is it possible to make central function which overwrite the kendo datasoruce headers that provides the token to the request headers? because i have more than 600 datasources, I just want to have token setup in one place.

like image 384
adnan Avatar asked Jul 11 '17 10:07

adnan


1 Answers

Yes, you can globally set a specific header every time you send a request. Try this one,

$(document).ajaxSend(function (event, jqXHR, options) {
    jqXHR.setRequestHeader('Authorization', 'Bearer ' + accesstoken);
});
like image 123
Cara Avatar answered Nov 05 '22 00:11

Cara