views:

485

answers:

1

Hi! I have problem with xhr open() method. My code follows :

var xmlhttp=false;

if(!xmlhttp)
    try
    { 
        xmlhttp=new XMLHttpRequest(); 
    }
    catch(e)
    {
        xmlhttp=false;
    }

function returnPage(url)
{
    if(!xmlhttp)
        return alert("Your browser doesn't seem to support XMLHttpRequests.");

    xmlhttp.open("GET",url,true);
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState!=4) return;
        if(!xmlhttp.status||xmlhttp.status==200)
            alert(xmlhttp.responseText);
        else
            alert("Request failed!");
    }; //onreadystatechange

    xmlhttp.send(null);
}

Call :

<a href='#' onclick="returnPage('http://www.something.com'); return false;">Link 1</a></p>

I'm using IE8 (because I'm building web slice) and I received error "Access denied". I found on the Internet that problem is that XHR isn't working across different domains, but I used code from Firefox Add-on which is working OK. And that add-on and "my" code (which are the same) are calling the same page. How that add-on have access and my code not?

+4  A: 

Is the domain you're making the AJAX call to the same domain as your site? You cannot make requests to other domains.

EDIT:

Firefox Add-ons have elevated privileges (since the user has to install them). That is why the add-on can make cross-domain requests.

Matt
No, and I was aware of that problem. But I'm confused how Firefox add-on who also make XHR call to the "page" is working and mine code not? Code is the same.
rjovic
@rjovic - see my edit. Add-ons have elevated privileges since the end-user has to actually install the add-on.
Matt