简体   繁体   English

Kendo-UI 在传输中包含不记名令牌

[英]Kendo-UI Include Bearer Token in Transport

I am using Kendo-UI to display data in a ListView.我正在使用 Kendo-UI 在 ListView 中显示数据。 In the configuration, I have the DataSource setup like this:在配置中,我有这样的 DataSource 设置:

dataSource: {
    transport: {
        read: {
            contentType: 'application/json',
            dataType: 'json',
            type: 'GET',
            url: '/server/api/user/query.php/'
        }
    },
    pageSize: 20,
    schema: {
        data: 'records',
        id: 'UserId',
        model: {
            UserId: { editable: false, nullable: true },
            FirstName: { validation: { required: true } },
            LastName: { validation: { required: true } },
            Email: { validation: { required: true } },
            CreatedOn: { editable: false, type: 'date' },
            CreatedBy: { editable: false, type: 'number' },
            CreatedByFullName: { editable: false },
            ModifiedOn: { editable: false, type: 'date' },
            ModifiedBy: { editable: false, type: 'number' },
            ModifiedByFullName: { editable: false },
            DeletedOn: { editable: false, type: 'date' }
        },
        total: 'total'
    }
}

However, I need to specify the AJAX request's headers, specifically I need to set the bearer token in the authorization header using the following:但是,我需要指定 AJAX 请求的标头,特别是我需要使用以下命令在授权 header 中设置不记名令牌:

headers: {
    'Authorization': `Bearer ${utility.getJsonWebToken()}`
}

Looking at the documentation for the DataSource's Transport property ( here ), I don't see where I can specify any of the headers.查看 DataSource 的 Transport 属性的文档( 此处),我看不到在哪里可以指定任何标头。

A couple of other solutions, which don't need an $.ajax call since:其他一些解决方案不需要$.ajax调用,因为:

The data source uses jQuery.ajax to make an HTTP request to the remote service.数据源使用 jQuery.ajax 向远程服务发出 HTTP 请求。 The value configured via transport.read is passed to jQuery.ajax.通过 transport.read 配置的值被传递给 jQuery.ajax。 This means that you can set all options supported by jQuery.ajax via transport.read except the success and error callback functions which are used by the transport.这意味着您可以通过 transport.read 设置 jQuery.ajax 支持的所有选项,除了传输使用的成功和错误回调函数。

You can do something like:您可以执行以下操作:

transport: {
    read: {
        contentType: 'application/json',
        dataType: 'json',
        type: 'GET',
        url: '/server/api/user/query.php/',
        headers: {
            'Authorization': 'Bearer ' + utility.getJsonWebToken()
        },
    }
},

or或者

transport: {
    read: {
        contentType: 'application/json',
        dataType: 'json',
        type: 'GET',
        url: '/server/api/user/query.php/',
        beforeSend: beforeSend,
    }
},
...
function beforeSend(xhr) {
    xhr.setRequestHeader('Authorization', 'Bearer ' + utility.getJsonWebtoken());
}

The solution was to make the transport.read configuration a function:解决方案是使transport.read配置为 function:

transport: {
    read: function(options) {
        var queryString = new URLSearchParams(options.data);
        $.ajax({
            url: '/server/api/user/query.php/',
            method: 'GET',
            contentType: 'application/json',
            headers: {
                'Authorization': 'Bearer ' + utility.getJsonWebToken()
            },
            data: queryString.toString(),
            dataType: 'json'
        }).done(function(result) {
            options.success(result);
        }).fail(function(result) {
            options.error(result);
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM