Search code examples
javascriptphpsessionappcelerator

Multiple HTTP Client Requests not storing session data


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'     
        });

    };

Solution

  • 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.