0

I am trying to read .txt file using jquery from phonegap index file, but the jquery function returns nothing

Here is my code:

jQuery.get('http://gridberry.com/uploaded_files/E1BDA03F-3F9A-45E7-B16D-78F68C21DCD8.txt', function(data) {
   alert(data);
});

You can check the link of the txt file; it has data. What is the problem?

Pubby
  • 51,882
  • 13
  • 139
  • 180
Man Mann
  • 423
  • 2
  • 10
  • 17
  • You're likely running into the same origin policy. See this: http://stackoverflow.com/questions/2697557/accessing-web-service-from-jquery-cross-domain – Brian Feb 19 '13 at 02:13
  • I got 404 when I tried opening the text file in the browser. Also did you try running this in the simulator. You will not face cross-origin issues in the simulator so this should work. But please check the link of the file first. – Manish Kumar Feb 19 '13 at 06:56

4 Answers4

2

You can't use ajax requests to access data from a different domain. An easy workaround to this is make a PHP script that downloads the contents from that URL like so.

echo file_get_contents('http://gridberry.com/uploaded_files/E1BDA03F-3F9A-45E7-B16D-78F68C21DCD8.txt');

And .get() the script on your server.

Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
2

Trying your code in JSfiddle gave me this error:

XMLHttpRequest cannot load http://gridberry.com/uploaded_files/E1BDA03F-3F9A-45E7-B16D-78F68C21DCD8.txt. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

This is a classic cross-domain problem... you can't call an url outside your domain that way.

Etienne Desgagné
  • 3,102
  • 1
  • 29
  • 36
  • Can you use jsonp format instead of plain text? Ajax call using jsonp datatype is one of the way that allow to overcome cross-domain issue (http://en.wikipedia.org/wiki/JSONP). – Etienne Desgagné Feb 19 '13 at 03:29
1

its probably a cross domain call,hence its failing, you can do either of 2 things. 1.) Try to do Jsonp to make this call so that you can get the cross domain call working.

2)put a script tag with source as your url and read the script tag inner content in your javascript

0

try jQuery('#where_it_displays').load(file_url_here);

(jQuery doc)

Josh Austin
  • 740
  • 3
  • 14