I'm building a quick website; because of its nature I can afford to host it in my Dropbox public folder. I'm trying to read some text files using XMLHttpRequest:
function getParameters() {
var oRequest = new XMLHttpRequest();
var sURL = "http://dl.dropbox.com/u/7828009/Relay%20Fundraising%20Site/parameters.txt";
// var sURL = "file:///parameters.txt";
oRequest.open("GET",sURL,false);
oRequest.onreadystatechange = function (oEvent) {
if (oRequest.readyState === 4) {
if (oRequest.status === 200) {
console.log(oRequest.responseText)
} else {
console.log("Error", oRequest.statusText);
}
}
};
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
try {
oRequest.send(null)
} catch (err) {
alert(err);
}
if (oRequest.status==200) alert(oRequest.responseText);
else alert("Error executing XMLHttpRequest call!");
}
I'm getting an XMLHttpRequest 101 error in the send() line and I have no idea how to resolve this. The exact error is
XMLHttpRequest cannot load http://dl.dropbox.com/u/7828009/Relay%20Fundraising%20Site/parameters.txt. Origin null is not allowed by Access-Control-Allow-Origin.
Can I do anything to fix this, or is it because of Dropbox themselves?
http://dl.dropbox.com/u/7828009/Relay%20Fundraising%20Site/main.htm
New error is now
XMLHttpRequest cannot load http://www.javascripter.net/faq/requested_file.htm. Origin http://dl.dropbox.com is not allowed by Access-Control-Allow-Origin.
What can I do about this?
Update 2: D'oh moment…I had left in a test URL to see if it was a Dropbox issue. It linked to an external file. Fixing that fixed my issue.
The principal problem was running the file off the local copy. Running from the Dropbox public URL fixed the issue. Thanks Quentin, for indirectly leading me to the answer.