多个HTTP客户端请求未存储会话数据 [英] Multiple HTTP Client Requests not storing session data

查看:87
本文介绍了多个HTTP客户端请求未存储会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我想知道代码是否可能会加快其创建单独的会话ID的速度,让我详细说明一下.我有两个单独的HTTP客户端,一个又一个地执行(请参见下面的代码).我有一个奇怪的问题,在第二个HTTP客户端请求中,我正在做的就是检索某些会话数据.但是,有时返回的数据很好,而其他时候未定义会话信息,这不会导致任何问题.一旦我删除了第二个Http客户端,该问题就不再发生.

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.

我认为这可能归结于异步客户端,我是否可以将相同的Http客户端变量重新用于下一个操作,并将保留会话数据?任何建议或知识将不胜感激.

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

    };

推荐答案

您的问题是您同时执行两个调用.因此执行顺序未知.您需要做的是在第一个完成后调用第二个.为此,您需要在第一个回调中添加第二个http调用.

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

doFirstCallFunction然后获得一个回调函数,您应该在第一个进入http回调后调用该回调函数.

The doFirstCallFunction then gets a callback function, this callback function you should call after the first one has gotten into the http callback.

这篇关于多个HTTP客户端请求未存储会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆