代码之家  ›  专栏  ›  技术社区  ›  CryptoTitan

Sencha Architect 4-ExtJS 6.5->Dreamfactory 2.9身份验证会话令牌

  •  0
  • CryptoTitan  · 技术社区  · 7 年前

    我有点卡住了,我想征求你的意见,我正在尝试使用Sencha Architect中的DreamFactory设置一个登录窗口//身份验证,但我几乎是一个初学者。

    我当前的代码是:

    var username = Ext.getCmp('usr').getValue(),
    password = Ext.getCmp('pwd').getValue();
    
    Ext.Ajax.defaultHeaders = {
    'X-DreamFactory-Api-Key' : 'xyz',
    'Content-Type' : 'application/json'
    };
    
    Ext.Ajax.request({
    url: 'http://dreamfactoryaddy:8080/api/v2/user/session',
    withCredentials: true,
    email: username,
    password: password,
    cors: true,
    
    success: function(response) {
        var obj = Ext.decode(response.responseText);
        Ext.Ajax.defaultHeaders = {
            'X-DreamFactory-Api-Key' : 
    'xyz', 'Content-Type' : 'application/json', 'X-DreamFactory-Session-
    Token': obj.session_token
        };
        console.log(obj);
    },
    
    failure: function(response) {
            console.log(response.responseText);
    }
    
    });
    

    我收到此错误:

    {"error":{"code":401,"context":null,"message":"There is no valid session for the current request.","status_code":401}}
    

    http://prntscr.com/hate2e

    更新2:

                var username = Ext.getCmp('usr').getValue(),
                    password = Ext.getCmp('pwd').getValue(),
                    headers = {
                        'X-DreamFactory-Api-Key' : 'xyz',
                        'Content-Type' : 'application/json'
                    };
    
                Ext.Ajax.setDefaultHeaders(headers);
                Ext.Ajax.request({
                    url: 'http://chalton.swiftmedia.ca:8080/api/v2/user/session',
                    withCredentials: true,
                    jsonData: {
                        "email" : username,
                        "password" : password
                    },
                    cors: true,
    
                    success: function(response) {
                        var obj = Ext.util.JSON.decode(response.responseText),
                            newheaders = {
                                'X-DreamFactory-Api-Key' : 'xyz',
                                'Content-Type' : 'application/json',
                                'X-DreamFactory-Session-Token': obj.session_token
                            };
                        console.log(response.responseText);
                        Ext.Ajax.setDefaultHeaders(newheaders);
                    },
    
                    failure: function(response) {
                        console.log(response.responseText);
                    }
    
                });
            },
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Alexander    7 年前

    如果您检查浏览器控制台,您可能会发现 defaultHeaders 未正确应用于ajax请求。

    对于我想要作为默认头的任意其他头,我也有同样的问题。 Ext.Ajax.defaultHeaders = x 只是不起作用, Ext.Ajax.setDefaultHeaders(x)