并行ajax调用-无法从第一个接收响应 [英] parallel ajax calls - fail to receive response from the first

查看:43
本文介绍了并行ajax调用-无法从第一个接收响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个小的"ping"实用程序,以检查我们的两台服务器是否在线.

I'm implementing a little "ping" utility to check if our two servers are online.

这是JavaScript代码:

here is the javascript code:

var t1, t2, t3, t4;

function jsContactServers() {
    ajaxServerStatusWWW();
    ajaxServerStatusAPPS();
}

function ajaxServerStatusWWW() {
    try {                   
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        var t1 = setTimeout(function() {
            xmlhttp.abort();
            clearTimeout(t1);
            ServerIsDown("www");
        }, 7000);
        xmlhttp.onreadystatechange = function() {           
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var strOut;
                strOut = xmlhttp.responseText;
                console.log("www:" + strOut);
                if (strOut == "1") {
                    clearTimeout(t1);
                    document.getElementById("divwww").innerHTML = "www : UP";
                    document.getElementById("divwww").style.background = "green";
                    pauseSound("alarm_internet");
                    pauseSound("alarm_server");
                    setTimeout(ajaxServerStatusWWW, 10000);
                }
            }
        }
        console.log("www");
        xmlhttp.open("GET","http://www.mydomain.com/contactserver.php?IP=1.2.3.4",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send();
    }
    catch(err) {
        alert(err);
    }
}

function ajaxServerStatusAPPS() {
    try {                   
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        var t2 = setTimeout(function() {
            xmlhttp.abort();
            clearTimeout(t2);
            ServerIsDown("apps");
        }, 7000);
        xmlhttp.onreadystatechange = function() {           
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var strOut;
                strOut = xmlhttp.responseText;
                console.log("apps:" + strOut);
                if (strOut == "1") {
                    clearTimeout(t2);
                    document.getElementById("divapps").innerHTML = "apps : UP";
                    document.getElementById("divapps").style.background = "green";
                    setTimeout(ajaxServerStatusAPPS, 10000);
                }
            }
        }
        console.log("apps");
        xmlhttp.open("GET","http://www.mydomain.com/contactserver.php?IP=4.3.2.1",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send();
    }
    catch(err) {
        alert(err);
    }
}

contactserver.php尝试读取IP GET参数声明的服务器中的.php文件,如果可以读取php文件(服务器已启动),则返回"1".

the contactserver.php tries to read a .php file in the server declared in the IP GET parameter and returns "1" if the php file can be read (server is up).

现在,问题是,我确实收到了

Now, the problem is, I do get a reply from

ajaxServerStatusAPPS();

但是我没有得到的任何答复

ajaxServerStatusWWW();

[ console.log("www:" + strOut); 不会触发]

但是,如果我最初只调用ajaxServerStatusWWW()而不是两者,那么它将正常工作.如果我通过更改

However if I initially only call ajaxServerStatusWWW() instead of both, it works properly. It also works properly if i make the call synchronous instead of asynchronous by changing the

xmlhttp.open("...", true) ;

xmlhttp.open("...", false);

我在此过程中缺少什么吗?为什么会这样?

Am I missing something in the process? why is this happening ?

推荐答案

您正在成为

You're falling prey to The Horror of Implicit Globals: You don't declare xmlhttp, so it's an implicit global, and so the call to ajaxServerStatusAPPS overwrites the value that the call to ajaxServerStatusWWW stored in that variable. Both functions try to use the same variable.

ajaxServerStatusWWW ajaxServerStatusAPPS 内,使用 var 声明变量,以便它们各自拥有自己的变量.

Within ajaxServerStatusWWW and ajaxServerStatusAPPS, declare the variable using var so they each have their own.

在现代浏览器上,可以通过使用严格"模式使此操作成为错误,而不是隐式全局.在严格模式下分配未知标识符时,它会导致 ReferenceError 而不是创建全局标识符.

On modern browsers, you can make doing this an error rather than an implicit global by using "strict" mode. When you assign to an unknown identifier in strict mode, it causes a ReferenceError rather than creating a global.

这篇关于并行ajax调用-无法从第一个接收响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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