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

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

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

}

*** Sent via Developersdex http://www.developersdex.com ***
Feb 17 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Eric Wallstedt wrote:
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
Feb 17 '06 #2

P: n/a
VK

Eric Wallstedt wrote:
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?

Feb 17 '06 #3

P: n/a
Do you know of any workarounds?

Feb 20 '06 #4

P: n/a
Eric W wrote:
Do you know of any workarounds?


Are you referring to something?

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>

Anyway, for a workaround to a problem there has to be a problem first.
PointedEars
Feb 20 '06 #5

P: n/a

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.
*** Sent via Developersdex http://www.developersdex.com ***
Feb 22 '06 #6

P: n/a
Eric W wrote:
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.

| }
| // ...
| }
*** Sent via Developersdex http://www.developersdex.com ***


Don't. Use Google Groups if you have to, use a newsreader if you can.
PointedEars
Feb 22 '06 #7

P: n/a
> 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/de...4b0b2045de.asp
to see the list of xmlHttpReq.status codes

telmo

Feb 22 '06 #8

P: n/a
VK

Eric W wrote:
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);

Feb 23 '06 #9

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

Feb 23 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.