Skip to content Skip to sidebar Skip to footer

Javascript: Xmlhttprequest Randomly Stuck At Ready State 1

I've been working on a Windows gadget (meaning the 'browser' is Internet Explorer) that queries specified subnet addresses for information. Now, it sometimes does this at a relati

Solution 1:

Well it looks like I figured it out. I had a feeling it was an unresolved request, since it only happens when instances of it are closed (meaning that if one of them is closed while in communication with the server it stays in communication forever and no one else can access the server) and it appears that is the case. I made some modifications to the code in multiple areas and basically what it comes down to is when the gadget closes it makes sure to abort all of the requests. The requests are now instance variables (to allow for the aborting of them), but are still made new everytime they are needed.


Solution 2:

For those who stumble across this and need a concrete code example, here you go.

I had the same problem and the solution was to re-use the XMLHttpRequest object, to ensure that any previous request was cancelled before initiating a new one. This won't work if you want to have multiple AJAX requests flying around but in my case triggering a new request meant that the last one was no longer needed.

All of the requests on my page went through the same XMLHttpRequest wrapper method which looked like this;

//Declare the XMLHttpRequest object to be re-used
var global_xHttpRequest = null;

function xmlHttpHandler(type, params, complete, postData) {

   //Prevents an issue where previous requests get stuck and new ones then fail
   if (global_xHttpRequest == null) {
       global_xHttpRequest = new XMLHttpRequest();
   } else {
       global_xHttpRequest.abort();
   }

   //Parse out current URL
   var baseURL = window.location.host;
   var svc = "https://" + baseURL + "/processAction?";

   var url = svc + params;

   global_xHttpRequest.open(type, url, true);

   //Add the callback
   global_xHttpRequest.onreadystatechange = complete;

   global_xHttpRequest.send(postData);
}

Which can be used like this:

   xmlHttpHandler("GET", params, completeFnc);

Post a Comment for "Javascript: Xmlhttprequest Randomly Stuck At Ready State 1"