Forum Moderators: open

Message Too Old, No Replies

AJAX XMLhttpRequestObject causing errors

Not initialised?

         

trillianjedi

5:42 pm on Jul 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm struggling with a bug that I can't seem to be able to reliably and consistently repeat. I'm hoping that someone here will be able to look at my code and spot a gotcha?

What I'm trying to do is poll the server every 1/2 second for some content, then push that content to a DIV (straight forward AJAX really). This code originates from a book - can't remember which one now, but about 2 years old.

The code all works fine, except on rare occasion I get the following error in Firefox, and Firebug reports this:-

uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: javascript: eval(__firebugTemp__); :: anonymous :: line 1" data: no]

The error indicates that XMLHttpRequestObject hasn't been created yet (or not fully), but I would imagine this object to be persistent in memory? If it isn't, the intention of this code is to catch that with the checks against it equalling 'false', the value its variable container is initiated to when declared.

Confused.... ;)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>My Thing</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<LINK href="../css/main.css" type=text/css
rel=stylesheet><LINK href="../style.css"
type=text/css rel=stylesheet>

<script language="javascript">
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!XMLHttpRequestObject) {
alert('Failed to create XMLHTTP instance. Please contact support. Thank you.');
}


function getData(dataSource, divID) {
if (XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", dataSource, true);

XMLHttpRequestObject.onreadystatechange = function ()
{
if (XMLHttpRequestObject.readyState == 4 &&XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}

if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 404) {
obj.innerHTML = "No Data";
}
}

XMLHttpRequestObject.send(null);
}
}

var secs
var timerID = null
var timerRunning = false
var delay = 500

function InitializeTimer() {
// Set the length of the timer, in seconds
secs = 1
StartTheTimer()
}

function StartTheTimer() {
if (secs==0) {
getData('http://www.example.com/push.php', 'targetDiv')
if(timerRunning) {
clearTimeout(timerID)
timerRunning = false
InitializeTimer()
}
} else {
secs = secs - 1
timerRunning = true
timerID = self.setTimeout("StartTheTimer()", delay)
}
}
</script>

<body onLoad="InitializeTimer()">

HTML Code follows.....

eelixduppy

12:18 am on Jul 9, 2008 (gmt 0)



>> What I'm trying to do is poll the server every 1/2 second for some content

Alright, to be honest I do not really know the reason for this, but the ONLY thing I can think of right now is you are trying to send a request while the previous request is still busy fetching the data. You shouldn't be counting the 0.5 second until you get the readyState code of 4.

Try to make this change and see if it fixes this problem.

trillianjedi

9:08 am on Jul 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you are trying to send a request while the previous request is still busy fetching the data

Yup, spot on! Thank you.

Fixed (and substantially cleaned up) code below in case it's of use to anyone.


<script language="javascript">

var XMLHttpRequestObject = false;
var dataSource = '/push.php';
var delay = 1000;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!XMLHttpRequestObject) {
alert('Failed to create XMLHTTP instance. Please contact support. Thank you.');
}

function getData(divID)
{
if (!XMLHttpRequestObject) {
return;
}
var obj = document.getElementById(divID);

if (XMLHttpRequestObject.readyState > 0
&& XMLHttpRequestObject.readyState < 4) {
// A request is sent before and waiting for the response... so return it
return;
}

XMLHttpRequestObject.open("GET", dataSource, true);
XMLHttpRequestObject.onreadystatechange = function ()
{
if (XMLHttpRequestObject.readyState == 4) {
if (XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
if (XMLHttpRequestObject.status == 404) {
obj.innerHTML = "No Data";
}
}
}
XMLHttpRequestObject.send(null);

setTimeout("getData('targetDiv')", delay);
}
</script>

<body onLoad="getData('targetDiv')">

eelixduppy

2:12 pm on Jul 10, 2008 (gmt 0)



Nice work - glad that turned out to be the problem and it wasn't a wild goose chase ;)