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

本文是小编为大家收集整理的关于多个HTTP客户端请求不存储会话数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个问题,我想知道代码可能会禁食它创建单独的会话ID,让我详细说明.我有两个独立的HTTP客户端,它们彼此之间执行一个(请参见下面的代码).我遇到的奇怪问题是在第二个HTTP客户端请求中,我要做的就是检索一些会话数据.但是,有时它会返回数据罚款,而其他时候,会话信息不确定,这不会导致问题结束.一旦我删除了第二个HTTP客户端,就不再发生问题.

我认为这可能取决于异步客户端,我可以重新使用同一HTTP客户端变量以保留下一个操作和会话数据吗?任何建议或知识都将不胜感激.

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调用.

,为了使您的代码更有条理,我建议使用功能!使其也更可读.

function doBothCalls(){
    doFirstCallFunction(function(){
        doSecondCallFunction();
    }
}

doFirstCallFunction然后获取回调函数,在第一个回调后,您应该调用此回调函数.

.

其他推荐答案

您需要的东西称为 promistiss Javascript中.

当您进行异步调用时,它们都以随机的时间顺序进行,因此您不能进行异步调用,这取决于在相同的执行上下文中另一个异步调用的结果(您正在代码中执行此操作)

为了克服这一点,JavaScript具有promises的功能,这是

的.

承诺对象表示可能尚未可用的值,但将来将在某个时候解决.它使您可以以更同步的方式编写异步代码.例如,如果您使用Promise API对远程Web服务进行异步调用,则将创建一个Promise对象,该对象代表将来由Web服务返回的数据.

本文地址:https://www.itbaoku.cn/post/1938214.html

问题描述

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

    };

推荐答案

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.

其他推荐答案

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.