0

I have a jQuery load script that strangely works with a text file hosted in one location but not in another. For example, this:

JSFiddle

$(document).ready(function () {
    $("button").click(function () {
       $("#div10").load("https://dl.dropboxusercontent.com/u/29635158/replace.txt", function () {
          alert("Done Loading");
       });
    });
});

...works just fine and is able to load the .txt document from my Dropbox.

But this:

JSFiddle

$(document).ready(function () {
    $("button").click(function () {
        $("#div10").load("http://hs.biocanvas.net/files/replace.txt", function () {
           alert("Done Loading");
        });
    });
});

...is the exact same script with the exact same .txt file, except that .txt file is now hosted on a different server (and the URL accordingly changed in the script). However, the .txt file isn't loaded into the targeted div.

Both .txt files are readable if you copy + paste their URLs into a browser.

Any thoughts? Thanks for your help.

War10ck
  • 12,387
  • 7
  • 41
  • 54

2 Answers2

2

The problem could be a cross-domain request. If you are using PHP or other server technology, you need allow the permissions in your application, but if you are using phonegap, that not is necessary.

Access-Control-Allow-Origin: http://example.org/

Or, if it’s a public resource:

Access-Control-Allow-Origin: *

As reference, you can read this post: HTTP access control (CORS) Developer mozilla

or AJAX - Introducing Cross-domain Request (XDR)

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • Thanks for your quick response! I'm completely naive when it comes to this...where would I go about changing these permissions? – user3328873 Feb 20 '14 at 21:20
  • 1
    If you're using apache, for example, you could do it through an .htaccess file http://stackoverflow.com/questions/14467673/enable-cors-in-htaccess Other webservers have similar ways of setting these parameters. – Kevin B Feb 20 '14 at 21:22
  • Are you using PHP, .Net what technology? or only javascript? – Benjamin RD Feb 20 '14 at 21:22
  • The server is on Apache, I believe...I'm trying to edit the .htaccess – user3328873 Feb 20 '14 at 21:30
  • 1
    I set the header access and the trick was to empty my cache, then it worked. Thank you so much to everyone who helped! – user3328873 Feb 20 '14 at 22:01
0

A bit late - but just to confirm user3294396 answer, if you run the code and look in the console, you'll probably see an error like:

XMLHttpRequest cannot load http://hs.biocanvas.net/files/replace.txt. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

So you need to set up explicit access on the server.

edwh
  • 58
  • 1
  • 5
  • I've attempted to edit the .htaccess with Access-Control-Allow-Origin: * but it hasn't worked...I'm not sure what I'm doing wrong (and I'm sadly a complete novice at these things...) – user3328873 Feb 20 '14 at 21:42
  • If you're using Apache (and it sounds like you are) - could you post what you've added for .htaccess? Have you tried restarting apache after making the edit? – edwh Feb 20 '14 at 22:13
  • I've fixed it...I think it just took a while for my server to update the file, but this is what I added into each relevant directory into the .htaccess file to fix it: Header set Access-Control-Allow-Origin * – user3328873 Feb 21 '14 at 23:18