Solved

Javascript onReadyStateChange event not fire for XMLHttpRequest object?

Posted on 2015-01-25
2
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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