Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript onReadyStateChange event not fire for XMLHttpRequest object?

Posted on 2015-01-25
2
Medium Priority
?
346 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

670 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