troubleshooting Question

onreadystatechange Event Stops Firing

Avatar of Brian Bush
Brian BushFlag for United States of America asked on
JavaScript
8 Comments1 Solution1023 ViewsLast Modified:
I am trying to set up a couple of ajax functions to reuse the XMLHttpRequest object. In this example, I have a function that runs on an interval. It fires the event handler just fine the first time through. The second time, the ajax sends back to the server and returns data, but the event handler is not run. Any ideas why?
var xmlHttpRequest;
 
function ajaxGet(uri, isAsync) {
	if (typeof(xmlHttpRequest) == 'undefined') {
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			xmlHttpRequest = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
			try {
				xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
	}
	xmlHttpRequest.onreadystatechange = ajaxListener;
	xmlHttpRequest.open("GET", uri, isAsync);
	xmlHttpRequest.send(null);
}
 
function ajaxListener() {
	if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
		alert("response: " + xmlHttpRequest.responseText);
	}
}
 
function getHttpUpdates() {
	ajaxGet("AjaxServlet?do=getUpdates", true);
}
 
window.setInterval(getHttpUpdates, 5000);
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros