AJAX problem with firefox (no readyState == 3 ????)

E

Eric Wallstedt

I have a page that "logs" changes made to input fields using ajax to
pass data to a cgi. I use POST and it works fine most of the time (all
the time in IE). But it fails when I get the data from a popup that
invokes a function that in turn invokes ajax.

Scenerio

1. user clicks a button to create a popup

2. the user selects a select member and clicks a button and a
function in the calling page is invoked.

3. the ajax function is invoked and the data is passed to the cgi.

if it works I get:

request sent to server... 1... 2... 3... 4... 2006-02-17
13:28:47.491131|id:2097|name:dept|seq:0|value:53

when it fails I get:
request sent to server... 1... 2... 4...

where the 1... 2... 3... 4.... are the readyStates

Any ideas?

function rt_AJAX(req) {
document.getElementById("message").value = " request sent to
server... ";
xmlhttpPost(req);
}

function xmlhttpPost(req) {

var xmlHttpReq = false;

req.s1=true;
req.s2=true;
req.s3=true;
req.s4=true;

// IE
if (window.ActiveXObject) {
var xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
// Mozilla/Safari
else {
var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.overrideMimeType('text/xml');
}

xmlHttpReq.open('POST', req.url, true);

xmlHttpReq.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

xmlHttpReq.onreadystatechange = function() {
if (xmlHttpReq.readyState == 1) { req.s1=false; }
if (xmlHttpReq.readyState == 2) { req.s2=false; }
if (xmlHttpReq.readyState == 3) { req.s3=false; }
if (xmlHttpReq.readyState == 4) { req.s4=false; }
document.getElementById("message").value += xmlHttpReq.readyState
+ "... ";
if (xmlHttpReq.readyState == 4) {
try {
req.str=xmlHttpReq.responseText;
}
catch(e) {
req.str=e;
}
updatepage(req);
if (req.s1 || req.s2 || req.s3 || req.s4) {
alert('Update Failed on the Server ("'+req.name+'")
Discontinue Use and Contact XXXXXXXXX');
}
}
}

xmlHttpReq.send(getquerystring(req));

}
 
T

Thomas 'PointedEars' Lahn

Eric said:
if it works I get:

request sent to server... 1... 2... 3... 4... 2006-02-17
13:28:47.491131|id:2097|name:dept|seq:0|value:53

when it fails I get:

"It fails" is a broad subject.
request sent to server... 1... 2... 4...

where the 1... 2... 3... 4.... are the readyStates

Any ideas?

<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/0e6a34e4-f90c-489d-acff-cb44242fafc6.asp>
<URL:http://msdn.microsoft.com/library/en-us/xmlsdk/html/f6de15fc-72e9-418e-b275-d94b0b2045de.asp>

(yes, I have observed that this is about Firefox)


HTH

PointedEars
 
V

VK

Eric said:
I have a page that "logs" changes made to input fields using ajax to
pass data to a cgi. I use POST and it works fine most of the time (all
the time in IE). But it fails when I get the data from a popup that
invokes a function that in turn invokes ajax.

Scenerio

1. user clicks a button to create a popup

2. the user selects a select member and clicks a button and a
function in the calling page is invoked.

3. the ajax function is invoked and the data is passed to the cgi.

if it works I get:

request sent to server... 1... 2... 3... 4... 2006-02-17
13:28:47.491131|id:2097|name:dept|seq:0|value:53

when it fails I get:
request sent to server... 1... 2... 4...

where the 1... 2... 3... 4.... are the readyStates

Any ideas?

Numeric values of readyState are really IE's "table of states" index
values:

[1] : "loading" Object is loading its data.
[2] : "loaded" Object has finished loading its data.
[3] : "interactive" User can interact with the object even though it is
not fully loaded.
[4] : "complete" Object is completely initialized

So on error Firefox skips on "interactive" and it goes right to
"complete". We may discuss the sense of "complete" in application to a
failed request :), but it's not a Firefox problem. Microsoft did not
think as of necessary (or just did not think at all) to add the 5th
state like:
[5] : "failed"
and everyone else adopted it as it was, so now we have to check the
status on readyState==4.

I missed though what is your particular disconvenience of it?
 
E

Eric W

Do you know how I can trap the error in Firefox? I don't get an error
logged in the javascript console or a status back to check.
 
T

Thomas 'PointedEars' Lahn

Eric said:
Do you know how I can trap the error in Firefox?

Will you understand that there is no error at all?
I don't get an error logged in the javascript console

And that it is not something to be expected, provided your
code is syntactically correct.
or a status back to check.

Then either your server, your user agent or your code is borken.
Most certainly your server and your code are:

| req.s1=true;
| req.s2=true;
| req.s3=true;
| req.s4=true;
| // ...
| xmlHttpReq.onreadystatechange = function() {
| if (xmlHttpReq.readyState == 1) { req.s1=false; }
| if (xmlHttpReq.readyState == 2) { req.s2=false; }
| if (xmlHttpReq.readyState == 3) { req.s3=false; }
| if (xmlHttpReq.readyState == 4) { req.s4=false; }
| // ...
| if (xmlHttpReq.readyState == 4) {
| // ...
| if (req.s1 || req.s2 || req.s3 || req.s4) {

This evaluates to `true' if readyState == 3 ("User can interact with the
object even though it is not fully loaded.") did _not_ happen (because
req.sq3 is still `true' then). There is no reason why readyState == 3
former must happen, so

| alert('Update Failed on the Server ("'+req.name+'")
| Discontinue Use and Contact XXXXXXXXX');

is simply a wrong conclusion, especially given that readyState == 4
("'complete': Object is completely initialized") was already reached.

It only is missing logic in your source code that makes it seem _as if_
there was an error.

| }
| // ...
| }

Don't. Use Google Groups if you have to, use a newsreader if you can.


PointedEars
 
T

Telmo Costa

if (xmlHttpReq.readyState == 4) {


As VK pointed:

[1] : "loading" Object is loading its data.
[2] : "loaded" Object has finished loading its data.
[3] : "interactive" User can interact with the object even though it is
not fully loaded.
[4] : "complete" Object is completely initialized


readyState==4 does not say that there is an error or not, just that the
request is completed (with or without an error).
After (in) readyState==4, you should check for xmlHttpReq.status

Something like this:


if (xmlHttpReq.readyState == 4) {

if (xmlHttpReq.status == 200) {
// ok
} else {
// error
}

}


also, check this:
http://msdn.microsoft.com/library/d...html/f6de15fc-72e9-418e-b275-d94b0b2045de.asp
to see the list of xmlHttpReq.status codes

telmo
 
V

VK

Eric said:
Do you know how I can trap the error in Firefox? I don't get an error
logged in the javascript console or a status back to check.

If by "error" you mean data retrieval error from the server then:

IXMLHTTPRequest (IE) / XMLHttpRequest (others) has five readyStates
reported by onreadystatechange handler:

[0] : "uninitialized" : // I missed that one in the first post
[1] : "loading" Object is loading its data.
[2] : "loaded" Object has finished loading its data.
[3] : "interactive" User can interact with the object even though it is
not fully loaded.
[4] : "complete" Object is completely initialized

This is by the original IE's notation, but the exact sense of each
state has been adjusted rather creatively :) in wannabes'
implementations.

In the particular readyState==4 on the majority of implementations
simply means "The ajaxoid stopped any attempts to send/get data to/from
server for this particular transaction."

(readyState==4) says *absolutely nothing* wether it was a successfull
transaction or a failure.

Also as per
<http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/readyState_1.asp>

(wich is currently the only one official documentation for readyState -
not counting wiki'ed revelations):-
<q>The states through which an object passes are determined by that
object; an object can skip certain states (for example, interactive) if
the state does not apply to that object.</q>

Therefore the corrrect and only one available algorithm is to monitor
readyState==4 and check status property to determine if you are lucky
or not.

** No program execution logic can be connected with other readyState's
**

if (xhr.readyState == 4) {
if ((xhr.status == 200)||(xhr.status == 0)) {
// you are lucky :)
}
else {
// you are not
// study .status to know why
}
}

(xhr.status == 0) is needed only if debugging your ajaxoid locally
(file://) or if you want to use it both for server and local files:
because IE returns status 0 for local files.

There is also a known issue with Firefox: in case of server connection
failure (thus not 404 but something really yaky like timeout, unknown,
refused etc) ajaxoid gets unstable. It still calls onreadystatechange
handler but on first attempt to read any of his properties it throws an
error and stops the script execution.
Therefore you need to place the state/status check into try-catch
block. It will naturally lead to a syntax error on the older (really
old actually) systems:- but here is the exact situation "damned if you
do, damned if you don't". I would definitely vote for the modern
systems support - but the final choice is up to you. If you decide to
make your program robust for JavaScript 1.5 / JScript 5 and higher (all
systems over the last 6 years) then:

try {
// here you would get an error on Firefox if
// the connection failed
if (xhr.readyState == 4) {
if ((xhr.status == 200)||(xhr.status == 0)) {
// you are lucky :)
}
else {
// you are not
/ / study .status to know why
}
}
}
catch(e) {
// some serious connection error
// you have only guess about
}

And the last but not least: XMLHttpRequest wants send(null) for GET,
IXMLHTTPRequest wants send("") for GET. Otherwise you may get some
funny behavior in some funny situations. So somewhere in the init
block:

/*@cc_on @*/
/*@if (@_jscript)
var dummy = '';
@else @*/
var dummy = null;
/*@end @*/

so later:

xhr.send(dummy);
 
E

Eric W

It turns out that there is a bug in firefox regarding ajax and popup
windows....

Bugzilla Bug 317600
nsIXMLHttpRequest throws exception on access of status member when the
request originate from a popup windows

The workaround is have the parent window originate the request!

Thanks everyone for your input!
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top