简体   繁体   English

等待两个异步请求

[英]Waiting for two asynchonous requests

I'm having trouble sending two request in PHP and waiting for both answers. 我在用PHP发送两个请求并等待两个答案时遇到麻烦。 Here is my code : 这是我的代码:

        function getXDomainRequest() {
            var xdr = null;

            if (window.XDomainRequest) {
                xdr = new XDomainRequest();
            } else if (window.XMLHttpRequest) {
                xdr = new XMLHttpRequest({mozSystem: true});
            } else {
                alert("Your browser does not support AJAX");
            }

            return xdr;
        }

        function sendData() {
            var json1= "";
            var json2= "";

            var xdr = getXDomainRequest();
            xdr.onload = function() {
                json1 = xdr.responseText;
            }

            var xdr2 = getXDomainRequest();
            xdr2.onload = function() {
                json2 = xdr2.responseText;
            }

            var Id = document.querySelector('#searchField').value;
            // Call API
            xdr.open("GET", "./dorequest.php?id=" + Id + "&requesttype=player");
            xdr.send();

            xdr2.open("GET", "./dorequest.php?id=" + Id + "&requesttype=stats");
            xdr2.send();

            xdr.wait();
            // Display results
            getHtmlResults(jsonPlayer, jsonStats);
        }

As expected here the json1 and json2 are still empty when getHtmlResults is called. 如预期的json1 ,当getHtmlResultsgetHtmlResultsjson2仍然为空。 I could do it synchronously by calling the xdr2.send() into the xdr.onload and my final function in the xdr2.onload but I want to do it asynchronously to get a better response time. 我能做到同步调用xdr2.send()xdr.onload和我的最终功能xdr2.onload ,但我想这样做异步,以获得更好的响应时间。

Thanks ! 谢谢 !

(any other comment on the code is welcome I'm quite new to php :) ) (欢迎对代码进行任何其他注释,我对php还是很新的:))

EDIT : 编辑:

So I tryied using Ajax and it seems to work :) 所以我尝试使用Ajax,它似乎可以工作:)

            var jsonPlayer = "";
            var jsonStats = "";
            var steamId = document.querySelector('#searchField').value;
            $.when(
                $.ajax({url: "./dorequest.php?steamid=" + steamId + "&requesttype=playersummary",
                        success: function(response){ jsonPlayer = response; }}),
                $.ajax({url: "./dorequest.php?steamid=" + steamId + "&requesttype=csgostats",
                        success: function(response){ jsonStats = response; }}) ).done(
                function(player, stats) {
                            getHtmlResults(player, stats);
                        });

You can send both the calls ASync and have a function in both .onload which checks if the other request has completed. 您可以同时发送两个调用ASync,并在两个.onload中都具有一个函数,该函数检查另一个请求是否已完成。 So as soon as one of the onload finds that the other onload is done, you can call the getHtmlResults function. 因此,只要其中一个onload发现另一个onload完成,就可以调用getHtmlResults函数。

Promises are commonly used as an abstraction to deal with asynchronous processes. 承诺通常用作处理异步过程的抽象。

Wrap your AJAX calls in a Promise to do: 将您的AJAX呼叫包装在Promise中以执行以下操作:

var ajax1 = request("stats");
var ajax2 = request("player");
when(ajax1, ajax2).done(function (stats, player) {
    console.log(stats, player);
});

Most popular frameworks have a built-in Promise API. 大多数流行的框架都有内置的Promise API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM