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
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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.