I have a simple JavaScript function that opens an XML-File. The file URL is simply the one of the website displayed right now, obtained in the JavaScript file of a Firefox extension via
var url =content.document.location.href;
this works just fine, next,
xml=loadXMLDoc(url);
should give me this file, which works just fine on websites on the internet. however, I set up a local machine now, and instead of http:publicwebsite.com/file.rdf I now have http://localhost/file.rdf and suddenly the JavaScript function loadXMLDoc does not produce any result.
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
try
{
xhttp.open("GET",dname,false); // open server interface
}
catch (err)
{ // if error occurs
alert("XMLHttpRequest.open() failed.\n"+ err);
}
xhttp.open("GET",dname,false);
xhttp.send("");
alert(xhttp.responseXML);
return xhttp.responseXML;
}
the alert(xhttp.responseXML); returns null.
Is this a problem with the same origin policy or what did I miss?
You are writing an extension – there is no same-origin policy, you are allowed to read whatever you like. I rather suspect that the MIME type returned by your local server is incorrect, it needs to be an XML MIME type to be processed by XMLHttpRequest correctly (probably
application/rdf+xmlin this case). If you are using Apache the following directive in the server config should do:If you cannot influence the server but you are certain that what you get back is an XML file you can also use overrideMimeType: