问题
I got a little bit stuck and I would like to ask your advice, I am trying to setup a login window // authentication using DreamFactory in Sencha Architect however I am pretty much a beginner.
My current Code is:
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);
}
});
I receive this error:
{"error":{"code":401,"context":null,"message":"There is no valid session for the current request.","status_code":401}}
http://prntscr.com/hate2e
Can you give me some advice how to handle Sessions properly?
UPDATE 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:
If you check the browser console, you may find that the defaultHeaders
are not correctly applied to your ajax requests.
I had the very same issue with arbitrary other headers I wanted as the default headers. Ext.Ajax.defaultHeaders = x
just doesn't work, Ext.Ajax.setDefaultHeaders(x)
does.
来源:https://stackoverflow.com/questions/47310459/sencha-architect-4-extjs-6-5-dreamfactory-2-9-authentication-session-token