如果多个异步请求,XMLHttpRequest响应失败

时间:2015-03-05 11:48:39

标签: javascript xmlhttprequest

我定期向网络服务器发送请求。如果我每x秒发送一个请求按预期工作。但是如果每x秒发送四个请求,只需获得响应'xmlhttp.readyState = 1'

function send(value) 
    {

        if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {

                console.log("Resp from " + value + ":='" + xmlhttp.responseText + "' readyState="+xmlhttp.readyState + " status=" + xmlhttp.status);

                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {           
                        //Process response


                    }
            }
            xmlhttp.open("GET", "http://localhost:8080?"+value, true);
            xmlhttp.send();     

    }

示例OK:

这里我得到xmlhttp.readyState = 1,2,3,(4 + response)

setInterval( function() { send("cmd=0"); }, 1000 );

示例失败:

我在所有回复中都得到'xmlhttp.readyState = 1'。

setInterval( function() { send("cmd=0"); }, 1000 );
setInterval( function() { send("cmd=1"); }, 1000 );
setInterval( function() { send("cmd=2"); }, 1000 );
setInterval( function() { send("cmd=3"); }, 1000 );

请求在服务器中正确处理

的信息:

  • readyState = 1 - > OPENED
  • readyState = 2 - > HEADERS_RECEIVED
  • readyState = 3 - > LOADING
  • readyState = 4 - > DONE

0 个答案:

没有答案
相关问题