Man Mann
Man Mann

Reputation: 423

Read txt file using Jquery on phonegap

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?

Upvotes: 0

Views: 508

Answers (4)

Etienne Desgagné
Etienne Desgagné

Reputation: 3252

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.

Upvotes: 2

Sundar Iyer
Sundar Iyer

Reputation: 11

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

Upvotes: 1

Josh Austin
Josh Austin

Reputation: 776

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

(jQuery doc)

Upvotes: 0

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

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.

Upvotes: 2

Related Questions