JavaScript » Ajax

ID #1009

I have an Ajax request. Why does the onreadystatechange callback is not fired on Firefox?

If the Ajax request is fired synchronously, the onreadystatechange callback will not be fired on Firefox. I'm not sure if it is an issue or a design feature.

The semantics of the Ajax request are as follow:

var request = getRequestObject();
request.onreadystatechange = function()
{
if(request.readyState == 4)
{
alert('Request completed');
}
}
request.open('GET', 'MyURL.php', false);
request.send(null);

Notice a very important deviation from the common Ajax requests... the 3rd paramter is false - indicating that the request is a synchronous call.

The following observations is made:

In Firefox, the code after call to send method is executed only after the data is received from the server and also the onreadystatechange callback is never called.

I will personally classify it a bug that the callback is not called since the readyState would be internally updated as the request is fired and data is received. As such, I would expect the callback to be called irrespective of whether it is a synchronous request or not.

Have a look at the test case attached with this FAQ entry.

So, whenever you fire the request in synchronous mode, ensure that you do care of this issue. The only workaround is to have your callback code called after send method.

attached files: test-case.html, result-ff.jpg, result-ie.jpg

Categories for this entry

Last update: 2008-06-29 12:53
Author: Edujini Labs Pvt Ltd
Revision: 1.3

Print this record Print this record
Send to a friend Send to a friend
Show this as PDF file Show this as PDF file
Export as XML-File Export as XML-File

Please rate this entry:

Average rating: 5 from 5 (2 Votes )

completely useless 1 2 3 4 5 most valuable

You can comment on this entry

Comment of vogue (2010-07-15 00:10:31):
vogue hi, it's vogue, thanks for your sharing

Comment of cheap uggs (2011-12-16 18:25:12):
fightingboer123 now they were surprised by the deserted and tranquil condition of the streets,GHD Pretty in Pink, distant roar,GHD Purple Indulgence Styler, however,GHD Red Lust Styler, they slipped along with great caution,cheap kensington ugg boots, On reaching the Rue Richelieu and finding her shop had not been disturbed,kensington ugg boots sale.