AJAX: sending xmlhttp requests in a loop ... but all responses not received

I have this function below and I call this function in a loop I get a warning n times, but only n-1 or sometimes n-2 answers

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()
                {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            alert(xmlhttp.responseText);
                            //document.getElementById("warnings_panel").innerHTML+=xmlhttp.responseText;
                        }
                }

                alert("in ajax) 

                xmlhttp.open("GET","getresponse.php?start="+ start
    + "&end=" + end,true);
                xmlhttp.send();
+3
source share
5 answers

It seems that you are dependent on one global variable xmlhttp. Do not do this.

+1
source

Make sure you send something unique with every request. IE sometimes caches responses to an AJAX request - if you add something unique to your request (for example, a counter that increments with each request), this will prevent IE from returning a cached response.

+1
source
<html>
<body>
</body>
<script type="text/javascript" defer>
function ajaxping() {

     var xmlhttp ={};

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

                 {

                         if (xmlhttp.readyState==4)

                         {

                         var status = xmlhttp.status;

                         var isSuccess = status >= 200 && status < 300 || status === 1223 || status === 304;

                                                                         if(isSuccess) {

                                                                         alert("got success response");

                                                                         }

                       }

                 }



                 alert("in ajax");



                 xmlhttp.open("GET","test.html");

                 xmlhttp.send();

}



var func = function() {

for(var i=0;i<10;i++){

ajaxping();

}

}

func();

</script>

</html>
+1

ajax- , , .

.

.

for(i=0; i<10; i++){
if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                        eval("xmlhttp"+i+"=new XMLHttpRequest()");
                }
                else
                {// code for IE6, IE5      
                }
                eval("xmlhttp"+i+".onreadystatechange=function(i)");
                function(var obj)
                {
                 //function contents
                }

                alert("in ajax) 

                // assign responsetext and send code
}
+1
xmlhttp.open("GET","getresponse.php?start="+ start
    + "&end=" + end,true); 

In the line above, you specified true as the last parameter, not false.

Thus, AJAX becomes a synchronous call, the interprint will wait until answers appear.

But for each response check for null.

+1
source

All Articles