问题
I have an issue and i wonder is it possible that the code is to fast that its creating separate session ids, let me elaborate. I have two separate HTTP Clients that perform one after each other (see code below). The strange issue i have is in the second HTTP client request all i am doing is retrieving some session data. However sometimes it returns the data fine and other times the session info is undefined, which is causing no end of problems. Once i remove the second Http client the issue no longer occurs.
A bit of research i think it could be down to asynchronous client, could i re-use the same Http client variable for the next operation and session data will be kept? Any suggests or knowledge would be much appreciated.
this.login = function(username, password, loaded, failed, incorrect) {
var xhr = Ti.Network.createHTTPClient({
onload : function(e) {
var response = this.responseText;
switch(response) {
case "1":
loaded();
break;
case "0":
incorrect();
break;
case "2":
incorrect();
break;
case "3":
incorrect();
break;
default:
failed();
}
},
onerror : function(e) {
failed(e);
},
timeout : 5000,
validatesSecureCertificate : false
});
xhr.open('POST', this.url, true);
xhr.send({
'action' : 'login',
'email' : username,
'password' : password,
});
var getdb = Ti.Network.createHTTPClient({
onload : function(e) {
var response = this.responseText;
Ti.App.Properties.setString('name', response);
},
onerror : function(e) {
failed(e);
},
timeout : 5000,
validatesSecureCertificate : false
});
getdb.open('POST', this.url, true);
getdb.send({
'action' : 'get_name',
'device' : 'mobile'
});
};
回答1:
Your problem is the fact that you're executing both calls at the same time. So the order of execution is unknown. What you need to do is call the 2nd after the first has finished. For this to work you will need to add the second http call within the callback of the first.
And to make your code more organised I recommend using functions! Makes it also more readable.
function doBothCalls(){
doFirstCallFunction(function(){
doSecondCallFunction();
}
}
The doFirstCallFunction
then gets a callback function, this callback function you should call after the first one has gotten into the http callback.
回答2:
What you need here is called Promises in Javascript.
When you do async calls, they all happen in random order of time , so you cannot do a async call which depends on result of another async call in same execution context(which you are doing in your code)
To overcome this, Javascript has functionality for promises
which in nutshell means:
A Promise object represents a value that may not be available yet, but will be resolved at some point in the future. It allows you to write asynchronous code in a more synchronous fashion. For example, if you use the promise API to make an asynchronous call to a remote web service you will create a Promise object which represents the data that will be returned by the web service in future.
来源:https://stackoverflow.com/questions/39877168/multiple-http-client-requests-not-storing-session-data