AJAX Firefox/IE problem

B

bruce

I have a routine containing AJAX that will not work under Firefox but
does work under IE. I have the following debug code in the routine.

if(xmlhttp.readyState == 4){
alert("readyState: " + xmlhttp.readyState);
alert("status: " + xmlhttp.status);
}

The first alert works. The second works under IE. Under Firefox an
error is returned in the Error Console, xmlhttp is not defined.

Can someone tell me what I'm doing wrong?

Thanks...

Bruce
 
J

Jeff North

On Fri, 20 Aug 2010 19:09:06 -0700 (PDT), in comp.lang.javascript
bruce <[email protected]>
| I have a routine containing AJAX that will not work under Firefox but
| does work under IE. I have the following debug code in the routine.
|
| if(xmlhttp.readyState == 4){
| alert("readyState: " + xmlhttp.readyState);
| alert("status: " + xmlhttp.status);
| }
|
| The first alert works. The second works under IE. Under Firefox an
| error is returned in the Error Console, xmlhttp is not defined.
|
| Can someone tell me what I'm doing wrong?
|
| Thanks...
|
| Bruce

How is the xmlhttp defined? IOW we need the full script to see what is
going on. Best bet is that you are not catering for non-IE browsers.
 
B

bruce

@bellsouth.net>
<cdf931f6-054f-41ac-9f5a-43441e6f2...@a36g2000yqc.googlegroups.com>
wrote:




How is the xmlhttp defined? IOW we need the full script to see what is
going on. Best bet is that you are not catering for non-IE browsers.

Here is the routine I use to define xmlhttp. Note that in the code of
my original message is debug code that I am using to try to find the
cause of my problem.

if(xmlhttp.readyState == 4){In that debug code the "if" statement and the first alert work. It is
the second alert that gives me the Firefox error.

function getxmlhttp (){
//Create a boolean variable to check for a valid Microsoft active x
instance
var xmlhttp = false;

//Check if we are using internet explorer
try {
//If the javascript version is greater than 5
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//If not, then use the older active x object
try {
// If we are using internet explorer
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
//Else we must be using a non-internet explorer browser
xmlhttp = false;
}
}

// If not using IE, create a
// JavaScript instance of the object.
if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}

return xmlhttp;
}

Thanks for the help...

Bruce
 
D

Denis McMahon

if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {

is typeof a function?

Try:

if (!xmlhttp && window.XMLHttpRequest!=undefined) {

or even

if (!xmlhttp && window.XMLHttpRequest) {

Rgds

Denis McMahon
 
M

Mel Smith

Bruce said:

// If not using IE, create a
// JavaScript instance of the object.
if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}

return xmlhttp;
}

Bruce:

Perhaps you're missing a pair of brackets ???

As in:

if (!xmlhttp && (typeof XMLHttpRequest != 'undefined' ) ) {

^
^

-Mel
 
B

bruce

First of all you could simply try:

if (xmlhttp.readyState == 4) {
  alert("readyState: " + xmlhttp.readyState +
    "\r\nstatus: " + xmlhttp.status);

}

For a functioning Ajax example, checkhttp://winhlp.com/node/684
.

Hans-Georg

Yes.. I know. This is debug code and I wanted to explicit alerts.. The
first one worked. The second one failed.
 
B

bruce

is typeof a function?

Try:

if (!xmlhttp && window.XMLHttpRequest!=undefined) {

or even

if (!xmlhttp && window.XMLHttpRequest) {

Rgds

Denis McMahon

Both of your suggestions fail with Firefox and work with IE8.

Remember, this is my debug code..

if(xmlhttp.readyState == 4){
alert("readyState: " + xmlhttp.readyState);
alert("status: " + xmlhttp.status);
}

The "if" statement works. So I would think xmlhttp is defined
correctly.
The first alert works and displays "readyState: 4" as I would expect
since that is the requirement of the "if" statement
The second alert causes Firefox to return "xmlhttp is not defined."
With IE8, the second alert displays "status: 200"

Suggestions on this problems are GREATLY appreciated...
Thanks...

Bruce
 
B

bruce

First of all you could simply try:

if (xmlhttp.readyState == 4) {
  alert("readyState: " + xmlhttp.readyState +
    "\r\nstatus: " + xmlhttp.status);

}

For a functioning Ajax example, checkhttp://winhlp.com/node/684
.

Hans-Georg

I did change my debug code to agree with your suggestion. Firefox
still fails and IE8 works..
BUT, the results are a little different. The output from the alert is:
readyState: 4
status: 0

And the same failure, "xmlhttp is not defined" now occurs on my next
statement:

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

This statement is not debug code but the actual routine code.

Here is my processajaxcode..

//function to process an XMLHttpRequest
function processajax(serverPage, obj, getOrPost, parmStr) {

//Get an XMLHttpRequest object of use.
xmlhttp = getxmlhttp();
if(getOrPost == "get") {

serverPage += "?" + parmStr;

xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {

// The following 4 lines of code are here for debug purpose.

if(xmlhttp.readyState == 4){
alert("readyState: " + xmlhttp.readyState + "\r\n" +
"status: " + xmlhttp.status);
}

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

response = xmlhttp.responseText;

alert("Response: " + response);

errreturn = response.substr(0,1); // Error if the message is
prefaced by '0'

if(errreturn==0){
alert("Error");
obj = document.getElementById('emsg');
response = response.substring(2);
}

obj.innerHTML = response;
}
}
xmlhttp.send(null);

} else {
xmlhttp.open("POST", serverPage, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-
urlencoded; charset=UTF-8");
xmlhttp.setRequestHeader("Content-length", parmStr.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(parmStr);
}

}
 
T

Thomas 'PointedEars' Lahn

Denis said:
is typeof a function?

`typeof' is a built-in operator in JavaScript since version 1.1, JScript
since version 1.0, and conforming implementations of ECMAScript Edition 1
and later.

Try:

if (!xmlhttp && window.XMLHttpRequest!=undefined) {

or even

if (!xmlhttp && window.XMLHttpRequest) {

No, do not do that. `XMLHttpRequest' is supposed to refer to a host object,
and so should be avoided to be subject to a type-converting test. And it
does not need to be a property of the object referred to by `window'. Also,
`undefined' can be overwritten in implementations of ECMAScript up to
Edition 3.

Bruce's original approach is generally sound but insufficient: one should
catch any exceptions the supposed constructor call might throw. For it does
not follow that `new XMLHttpRequest' must be successful if `new
ActiveXObject(…)' succeeded and the property value is different from the
original `undefined' value.

I would also recommend assigning `null' (or nothing) instead of `false' to
`xmlhttp' since this is about an object reference and not a primitive
boolean value, and use a lowercase `e' since it would not refer to a
constructor. The code comment about the "javascript version" is nonsense,
too (IE/MSHTML supports _JScript_).

Please refrain from giving further advice until you have covered the basics.

<http://jibbering.com/faq/>


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Bruce's original approach is generally sound but insufficient: one should
catch any exceptions the supposed constructor call might throw. For it
does not follow that `new XMLHttpRequest' must be successful if `new
ActiveXObject(…)' succeeded and the property value is different from the
^^^^^^^^^
s/succeeded/failed (threw an exception)/
original `undefined' value.


PointedEars
 
B

bruce

                    ^^^^^^^^^
s/succeeded/failed (threw an exception)/


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

I think I have changed my getxmlhttp routine to agree with your
suggestions. My main webpage still fails with FireFox and succeeds
with IE8. I also have the debug statements:

if(xmlhttp.readyState == 4){
alert("readyState: " + xmlhttp.readyState);
alert("status: " + xmlhttp.status);
}


The return from Firefox is "readyState: 4" and for the 2nd alert, the
Error Console returns "xmlhttp is not defined".

From IE8 I get "readyState: 4" and "status: 200".

Of course,the readyState 4 is expected otherwise the 'if' statement
would fail.

Here is my updated getxmlhttp();

function getxmlhttp (){
//Create a boolean variable to check for a valid Microsoft active x
instance
var xmlhttp = null;

//Check if we are using internet explorer
try {
//If the javascript version is greater than 5
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//If not, then use the older active x object
try {
// If we are using internet explorer
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
//Else we must be using a non-internet explorer browser
xmlhttp = null;
}
}

// If not using IE, create a
// JavaScript instance of the object.
if(xmlhttp == null && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
xmlhttp = null;
}
}

return xmlhttp;
}

So, my problem remains. What do I need to do to make my code work in
Firefox?

Thank you very much for the help...

Bruce
 
B

bruce

I think I have changed my getxmlhttp routine to agree with your
suggestions.  My main webpage still fails with FireFox and succeeds
with IE8.  I also have the debug statements:

        if(xmlhttp.readyState == 4){
                alert("readyState: " + xmlhttp.readyState);
                alert("status: " + xmlhttp.status);
        }

The return from Firefox is "readyState: 4" and for the 2nd alert, the
Error Console returns "xmlhttp is not defined".

From IE8 I get "readyState: 4" and "status: 200".

Of course,the readyState 4 is expected otherwise the 'if' statement
would fail.

Here is my updated getxmlhttp();

function getxmlhttp (){
        //Create a boolean variable to check for a valid Microsoft active x
instance
        var xmlhttp =  null;

        //Check if we are using internet explorer
        try {
                //If the javascript version is greater than 5
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                //If not, then use the older active x object
                try {
                        // If we are using internet explorer
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                        //Else we must be using anon-internet explorer browser
                        xmlhttp = null;
                }
        }

        // If not using IE, create a
        // JavaScript instance of the object.
        if(xmlhttp == null && typeof XMLHttpRequest != 'undefined') {
                try {
                xmlhttp = new XMLHttpRequest();
                } catch(e) {
                        xmlhttp = null;
                }
        }

        return xmlhttp;

}

So, my problem remains. What do I need to do to make my code work in
Firefox?

Thank you very much for the help...

Bruce

Since I haven't gotten a response in a couple of day, is it reasonable
to assume that no one can suggest anything for why my code works in
IE8 but not in Firefox?

Thanks....

Bruce
 
G

Gregor Kofler

Am 2010-08-28 02:41, bruce meinte:
Since I haven't gotten a response in a couple of day, is it reasonable
to assume that no one can suggest anything for why my code works in
IE8 but not in Firefox?

Thanks....

Simply put: I can't reproduce it.

My code looks like this:

[...]
if(http.readyState === 4) {
if(http.status >= 200 && http.status < 300 || http.status === 1223) {

// added to retrieve property values a second time
alert(http.readyState);
alert(http.status);
[...]
}
}
works (and without the alert() calls) has worked for - well - years, on
various Firefox versions.

Gregor
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top