Firefox XMLHttpRequest onreadystatechange handler not called

P

petermichaux

Hi,

I thought it is about time I tried writing some JavaScript with
XMLHttpRequest instead of just using the Yahoo! UI library. The simple
page below works in both Safari and Opera but I don't see the alert in
Firefox. Looking in Firefox's firebug plugin, I see that the request is
successfully sent and the correct response is received. Any ideas what
is wrong?

Thank you,
Peter


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple XMLHttpRequest</title>

<script type="text/javascript">

function makeRequest(method, url) {
var request = new XMLHttpRequest();

request.onreadystatechange = function() {
if (request.readyState == 4) {
// alert happens in Safari and Opera
// alert doesn't happen in Firefox
alert('handling readyState 4')
}
};

request.open(method, url);
request.send(null);
};

</script>

</head>

<body>

<p id="one">
<a href="#" onclick="makeRequest('GET', '/front/update'); return
false;">
do it!
</a>
</p>

</body>
</html>
 
R

Richard Cornford

Hi,

I thought it is about time I tried writing some JavaScript
with XMLHttpRequest instead of just using the Yahoo! UI
library. The simple page below works in both Safari and
Opera but I don't see the alert in Firefox. Looking in
Firefox's firebug plugin, I see that the request is
successfully sent and the correct response is received.
Any ideas what is wrong?

Without reading any more I would guess that the answer is that you are
making a synchronous request.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Simple XMLHttpRequest</title>

<script type="text/javascript">

function makeRequest(method, url) {
var request = new XMLHttpRequest();

request.onreadystatechange = function() {
if (request.readyState == 4) {
// alert happens in Safari and Opera
// alert doesn't happen in Firefox
alert('handling readyState 4')
}
};

request.open(method, url);
<snip>

It may also be an idea to move the assignment of the handler to after
the - open - call as I have a vague recollection of someone's
documentation saying that - open - strips the handler in their xml http
request objects. But you don't have a third argument for - open - and so
undefined may be taken as false, which equates to synchronous requests.
Mozilla does not call the readystate handler for synchronous requests so
you do something like:-

xmlhttp.open(type, URL, asynchronous);
if(asynchronous){
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
callbackFnc(xmlhttp);
xmlhttp.onreadystatechange = retFalse;
xmlhttp = null;
}
};
}
xmlhttp.send((data||null));
if(!asynchronous){
callbackFnc(xmlhttp);
xmlhttp = null;
}

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top