Can anyone please assist with AJAX using Netscape/Firefox?

Hi there,

I have been developing a new application which employs the (new to me!) AJAX technologies/structure to avoid document refresh. Everything I have done thusfar works brilliantly in IE, but fails when I test using either NS7 or FF1. I have been to almost every link I have found at Rich Schwartz’s page (http://www.rhs.com/poweroftheschwartz/htdocs/LotusDominoAjax.htm), but simply cannot get the code to work. I am hoping that someone here may be able to point out some glaring error that I have made and set me straight.

Using all of the examples I have read, I created a javascript function to return the XMLHTTPObject…

function getXMLHTTP() {

var A = false;

try {

A = new ActiveXObject( “Msxml2.XMLHTTP” );

} catch(e) {

try {

A = new ActiveXObject(“Microsoft.XMLHTTP”);

} catch(oc) {

A = false;

}

}

if( !A && typeof XMLHttpRequest != “undefined” ) {

A = new XMLHttpRequest();

}

return A;

}

That works fine x-browser, and returns the object OK.

Here is the code excerpt which uses that function and attempts to send the request…

// Prepare the XMLHTTP object for an HTTP POST

xmlhttp = getXMLHTTP();

var sURL = “ABC (Australian Broadcasting Corporation)”;

xmlhttp.open( “GET”, sURL, false );

xmlhttp.onreadystatechange = manageStateChange;

// Execute the request

try {

xmlhttp.setRequestHeader( ‘If-Modified-Since’, ‘Fri, 31 Dec 1999 00:00:00 GMT’ ); // Eliminate cacheing issues

xmlhttp.send(null);

alert( “XML request sent” );

}

catch (e) {

alert( “Could not calculate at this time.”);

}

And here is the code which manages the state change…

function manageStateChange() {

alert( "Ready state changed to: " + xmlhttp.readyState );

switch( xmlhttp.readyState ) {

case 2, 3:

// Display a progress indicator

document.body.style.cursor=‘wait’;

break;

case 4:

document.body.style.cursor=‘default’;

thisDoc.Results1.value = xmlhttp.responseText;

break;

}

}

Some interesting observations…

If I use the command “xmlhttp.send()”, then the browser immediately returns the message “Could not calculate at this time”. If I use the command “xmlhttp.send(null)”, then the browser returns the message “XML request sent”, but does absolutely nothing.

Using IE when the script is initiated, I receive the messages “Ready state changed to: 1”, “Ready state changed to: 2”… until the ready state changes to 4, THEN I receive the message “XML request sent”. However, I never see these “ready state” messages with the other browsers (but I DO see the “XML request sent” message).

I would really appreciate some insight from those of you more “in the know” than I.

If I have neglected an important piece of code (in the interest of keeping this as short as possible), then please let me know and I’ll post more.

Cheers!

T.

Subject: One issue down - Just a (small?) one to go…

I wonder if those of you who have been assisting me with this post (and thanks again!) have any insight into the resolution of my follow-on issue (trying to use the contents of the returned XML document)…

https://www-10.lotus.com/ldd/nd6forum.nsf/DateAllThreadedweb/1687e474a5779a4485257103001b0e03?OpenDocument

I’d sure appreciate a few additional moments of your time.

Cheers!

T.

Subject: Can anyone please assist with AJAX using Netscape/Firefox?

check this following url

http://www.dominodesigner.com/ddesigner/home.nsf/546c8633b1f2ca108725707a0023c00a/517494b9b591dcf6872570fa00123d85!OpenDocument

Note: You don’t have user name and password then register.

Thanks

jana

http://www.dominodesigner.com

cts_janardan@dominodesigner.com

Subject: RE: Can anyone please assist with AJAX using Netscape/Firefox?

Thanks for your response Johon.

I had already registered on your site, and have reviewed that document.

The only difference I see is that you differentiate your “xmlhttp.send” command to include the “null” parameter for Mozilla browsers.

Unfortunately, I had recognized that but still cannot solve my issue.

I think you’ll find that you can include the “null” parameter for all browsers (as far as I can see).

Thanks again tho!

T.

Subject: Can anyone please assist with AJAX using Netscape/Firefox?

As far as I know, the case statement doesn’t support multiple values in ECMAScript (although it may do in JScript, Microsoft’s flavour of the language), so if you make your 2 and 3 separate cases or use the default: case instead, it may keep the code from breaking.

Subject: RE: Can anyone please assist with AJAX using Netscape/Firefox?

Thanks for the reply Stan.

Unfortunately however, that didn’t work.

I removed all “case” statements except for 4.

I then removed the “switch” statement altogether and used an “if” statement instead.

All to no avail.

The funny thing is that I don’t receive the “Ready state changed to…” alerts with NS or FF, but I do with IE.

I’ll keep looking!

Subject: RE: Can anyone please assist with AJAX using Netscape/Firefox?

Could it be that the event isn’t triggering before 4? I’ve always used the if structure and only tested for 4 and a status of 200/other than 200. I can’t see much point in testing for anything else – your code knows when the request was initiated (if you want to set a “loading” UI event), and a 4 is the first opportunity you get to do something with the return. Not that it shouldn’t be giving you the state details, just that in the end there’s not a lot you can do with any of it.

Subject: RE: Can anyone please assist with AJAX using Netscape/Firefox?

I understand your point Stan.In fact, I use the other states to simply change the cursor.

Here is a little more of my code for clarification…

switch( xmlhttp.readyState ) {

case 2:

document.body.style.cursor=‘wait’;

break;

Thanks for your post tho - I almost forgot to include the check for a status of 200!

Cheers!

T.

Subject: RE: Can anyone please assist with AJAX using Netscape/Firefox?

That’s what I mean – you know when you launch the request, so there’s no reason to rely on the readystate to change the cursor to “wait” – just do it on send().

Subject: Aha!

Sorry guys (Stan/Johon), but I have finally noticed my (glaring) mistake.

Change the “false” parameter (async) in this line…

xmlhttp.open( “GET”, sURL, false );

to “true”, and Bob’s your proverbial father’s brother!

Just one note Stan. I switched back to using the multi-value “case” statement, and everything worked OK. Should I steer away from that in the future?

Thanks again for the responses!

Subject: RE: Aha!

I would, simply because the standard prohibits it (ECMA 262). you’re right though – making a synchronous call instead of an async would make a difference, wouldn’t it?

Subject: RE: Aha!

That’s what I meant by “glaring”!

Thanks again Stan - I’ll change my code to ensure standards compliance.