Jump to content

works in ie7 but not firefox ..??


severndigital

Recommended Posts

I have a simple piece of code

function getURL()
{
var xmlhttp = new XMLHttpRequest();
var url = "http://mydomain.org/service/";
xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	var returnValue = xmlhttp.responseText;
document.write(returnValue);
}

 

when I change the xmlhttp var to new ActiveXObject("Microsoft.XMLHTTP");

and run this in IE .. it works fine.

 

when I try to run it as is I get the following errors from firebug.

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: http://cyan/music-print/test :: getURL :: line 13"  data: no]

Line 0

 

everything shows up fine, when I try to access the url directly from firefox.

It is just a simple xml document.

 

Thanks in advance.

Chris

 

Link to comment
https://forums.phpfreaks.com/topic/221219-works-in-ie7-but-not-firefox/
Share on other sites

The problem is likely down to you trying to access xmlhttp.responseText before it's available. You need to bind the handling code to the 'onreadystatechange' event, and only attempt to use the response text once it's ready:

 

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var returnValue = xmlhttp.responseText;
        document.write(returnValue);
    }
}

 

That bound function will be called several times and only when the ready state is 4 (ready) will the response be handled. There's also a check to make sure the HTTP status returned was 200 (OK).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.