Interceptor for Authorization headers using Kendo UI datasource

a 夏天 提交于 2019-12-13 13:27:12

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/45031738/interceptor-for-authorization-headers-using-kendo-ui-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!