Solved

Javascript onReadyStateChange event not fire for XMLHttpRequest object?

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

Suggested Solutions

Title # Comments Views Activity
Add and remove classes and id's with jquery 2 2,868
I.E. 9 JQuery Menu disapears when page is refreshed? 3 106
hide show div element in asp.net 3 85
Problem with jscript mask. 3 129
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install

735 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