Solved

Javascript onReadyStateChange event not fire for XMLHttpRequest object?

Posted on 2015-01-25
2
274 Views
Last Modified: 2015-01-25
Here's the code:

//This will be our HTTP request object.
var xmlHTTP = createXMLHttpRequest();
var xmlCallback = null;
//Before setting up the send, idenfity the "callBack" function to be triggered.
xmlHTTP.onReadyStateChange = function(){
	//Here, we'll call the callback function passing the response XML.
		console.log("Test: 2");
	xmlCallback(xmlHTTP.responseXML);
}


//LoadXML will asynchronously load the XML file.
EcoBar.loadXMLData = function(filename, callBack) {
	
	xmlCallback = callBack;
  
	//Now, just call the "open" on the object (async true).
	xmlHTTP.open("GET", "ecobarRoot/xml/" + filename, true);
	
	console.log("Test: 1");
	
	//Now fire off the request.
	xmlHTTP.send();
}


//Simple function to try all the different methods to create the XML HTTP request.
function createXMLHttpRequest(){
  var xmlHTTP;
  try{xmlHTTP = new XMLHttpRequest();}
  catch(e){
    try{xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");}
    catch(e){
      try{xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");}
      catch(e){
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHTTP;
}

Open in new window


This should be fairly simple and straightforward, I would think.  I get the "Test 1" in the console, but never the "Test 2" and the application ceases to load.  (I have to wait to do anything until the XML comes in.)  So it's a show stopper.

If I switch to a synchronous load, it works just fine, but then the application is less responsive.  Any ideas?
0
Comment
Question by:Javin007
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40569791
The property name to set the event is defined as all lowercase so line 5 should be: xmlHTTP.onreadystatechange = ...

With this change your code (with the addition of the readyState property) logs:
Test: 2, readyState=1
Test: 1
Test: 2, readyState=2
Test: 2, readyState=3
Test: 2, readyState=4

Open in new window

So note that your function gets called several times, after each part of the download procedure is done.

For further examples, see: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest, you will probably just want to check for state 4 (done) in your function:
if (xmlHTTP.readyState == 4) {
  xmlCallback(xmlHTTP.responseXML);
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:Javin007
ID: 40569945
Thanks!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question