473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
9 15040
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
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
Do you know of any workarounds?

Feb 20 '06 #4
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

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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lodge.stuart | last post by:
Hi I've written a small AJAX-style UI - although it's not strictly AJAX as it uses HTML fragments rather than XML This UI works fine for me and the majority of users. However, a few users...
6
by: Nico VanHaaster | last post by:
Hello all, I have run across an issue with IE 6.0+. I have a page that makes an XMLHttpRequest to the webserver to update a report on the page. The first time you hit the refresh report button...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
1
by: wpt394 | last post by:
I am running a code that works just fine in Firefox, but seems to have trouble in IE. The basic idea is that a travel search process is initiated with one ajax call, and a 2nd ajax call 'updates'...
8
by: cyqotiq | last post by:
First, let me state that this is not necessarily a Firefox problem, as I haven't fully tested in IE just yet. Second, let me state that this is not the typical "getElementById not working Firefox"...
29
by: zalek | last post by:
I am writing application with Ajax in sync mode - xmlHttp.open("GET", url, false). I noticed that in FireFox handler doesn't starts. It starts when I use xmlHttp.open("GET", url,true). I need to...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.