Solved

Javascript onReadyStateChange event not fire for XMLHttpRequest object?

Posted on 2015-01-25
2
251 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now