I am trying to get JavaScript (using Greasemonkey) to retrieve data from my own site in order to set up another site. The code I use is as follows:
function getURL(url, func)
{
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function (e)
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
func(xhr.responseText, url);
}
else
{
alert(xhr.statusText, 0);
}
}
};
xhr.onerror = function (e)
{
alert("getURL Error: "+ xhr.statusText);
};
xhr.send(null);
}
The above works fine, it gets the text from the URL and returns it to an anonymous function, which I pass to the function if the file is in the same domain as the page I'm calling from. However, if the domain is different, then it works onerror.
How can I sort it so that I can retrieve data from another domain in this setting?
source
share