By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

Firefox XMLHttpRequest onreadystatechange handler not called

P: n/a
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>

Sep 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
pe**********@gmail.com wrote:
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.
Sep 13 '06 #2

P: n/a
have to call request.open() first
For example:
request.open(method, file);
pe**********@gmail.com wrote:
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>
Sep 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.