1

I'm experiencing an issue with XMLHttpRequest. If there is no Internet connection, my request is not timing out and thus freezing the browser. Here is the request code:

 var jsonData = new XMLHttpRequest();
 jsonData.open('GET', urltest, false);
 jsonData.Timeout = 100;
 jsonData.send(null);
 if(jsonData.status == 200) {
     alert(jsonData.statusText);
 }else{
     alert(jsonData.statusText);
 }

How do I abort the request if the server doesn't respond?

Pi Delport
  • 10,356
  • 3
  • 36
  • 50
lovinglinux
  • 123
  • 1
  • 5
  • 3
    See: [How to detect timeout on an AJAX (XmlHttpRequest) call in the browser?](http://stackoverflow.com/questions/1018705/how-to-detect-timeout-on-an-ajax-xmlhttprequest-call-in-the-browser) – Pi Delport Oct 14 '10 at 14:40
  • Thanks a lot. That was very helpful. The main issues is that I was using synchronous HttpRequest. Problem fixed. – lovinglinux Oct 15 '10 at 01:35

1 Answers1

0

I made some changes to your code, and I left some comments where I made changes explaining what's going on. I hope it helps!

var jsonData = new XMLHttpRequest();
jsonData.open('GET', urltest);/* I removed the false, because it kept the browser
waiting for the page to load to continue, and it didn't let it do other things
while it was still loading.*/
jsonData.Timeout = 100;
jsonData.send();
jsonData.onreadystatechange = function() {/*Each time readyState changes value,
                                            run this code.*/
    if (jsonData.readyState == 4) {/*When readyState is equal to 4,
                     the page has fully loaded.
                     Only run the code when it's 4,
                     not when it's still loading.*/
        if (jsonData.status == 200) {
            alert(jsonData.statusText);
        } else {
            alert(jsonData.statusText);
        }
        // More things to do when the page has fully loaded...
    }
}