extjs store.load传递授权标头没有流入

时间:2016-09-24 00:07:57

标签: ajax extjs proxy request-headers

我正在尝试通过参数将标头Authorization: 'Bearer <TOKEN>'传入我的store.load(),但它无效。

如果我这样做,它会起作用:

Ext.define('ExtApplication4.model.MenuListModel', {
    extend: 'ExtApplication4.model.Base',
    requires: [
        'ExtApplication4.model.Base'
    ],
    fields: [
        {
            name: 'text',
            type: 'string'
        },
        {
            name: 'iconCls',
            type: 'string'
        },
        {
            name: 'className',
            type: 'string'
        }
    ],
    proxy: {
        type: 'ajax',
        url: 'http://xxxxx/xxx/api/user/getusermenus/',
        reader: {
            type: 'json',
            rootProperty: 'data'
        },
        headers: {
            Authorization: 'Bearer ' + Ext.decode(Ext.util.Cookies.get('token'))
        }
    }
});

问题是我想填充store.load()中的授权标题。 我花了好几个小时试图找到这样做的语法。我尝试标题的所有内容都没有添加。

有人可以告诉我该怎么做吗?

这就是我的尝试:

targetStore.load({
    params: {
        username: uname
    },
    //headers: {            
    //    Authorization: 'Bearer ' + token;
    //},
    callback: function (records, operation, success) {

1 个答案:

答案 0 :(得分:6)

您无法在负载中执行此操作,就像您无法在负载中提供其他额外的内容一样。在加载时,您只能覆盖一小部分配置,但几乎不是全部。您必须在加载之前设置它:

store.getProxy().setHeaders({
    Authorization:'Bearer ' + ...
});
store.load({
    ...

如果你的ExtJS版本中有setHeaders is not a function,你可以设置

store.getProxy().headers = {
    Authorization:'Bearer ' + ...
};