2

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?


Update I've tried loading the file from the dropbox URL directly.
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.

Community
  • 1
  • 1
FeifanZ
  • 16,250
  • 7
  • 45
  • 84

2 Answers2

2

That error message is because you are hosting the HTML document on your local hard disk and not on the same server as the file you are trying to load.

You can't access resources like that for security reasons.


The URL http://www.javascripter.net/faq/requested_file.htm appears nowhere in the code you've shared. It should not produce that error.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    What he said, and also: http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Jack M Apr 29 '12 at 20:05
  • I've tried running it from the Dropbox public link, and so it should be the same origin, but I'm still getting a similar error. See updated post… Jack, thanks for the link, but I'm not sure I understand much of it. Perhaps some sample code would help. – FeifanZ Apr 29 '12 at 20:09
  • @Inspire48 — It should not be complaining about being unable to load a URL that you don't try to load. – Quentin Apr 29 '12 at 20:35
  • Oops, I was loading a file from another site that was describing how to use XMLHttpRequest, just to see if it was a DB issue. Apparently I had left it in and wasn't paying attention…and fixing that fixed my issue. So it appears that the problem was running it off the local file. Running it from the DB URL fixed the issue. – FeifanZ Apr 29 '12 at 20:37
0

I've used the same approach and it works flawlessly. You problem might be that Dropbox has changed the 'path' It now is https://dl.dropboxusercontent.com/u/...... and not http://dl.dropbox.com/u/

Christer
  • 41
  • 6