A .txt file on a remote server contains json syntax that I need to retrieve. I am using jsonp because it is on another domain.
I have tried like this :
$.getJSON('http://www.server1.com/json.txt'+'?callback=?', function(data){
console.log(data);
});
The .txt file is here But I get an error when the first name - value pair of the json is "parsed" :
Unexpected token :
I have tried changing the txt.json and added a var mydata={ jsonSyntax }
and this way I don't get the error but no data is logged to the console.
So how can I request and parse json.txt with jsonp?
jsonp works by wrapping the data on the server in a callback function. Most servers will allow you to specify the callback. Since you have a static text file, you need to hard-code a function name into the text file, then specify that name as a parameter in your ajax call.
Change the json.txt
file to this:
callback({ jsondata });
And your ajax call to this:
$.ajax({
...
dataType: 'jsonp',
jsonpCallback: 'callback'
}).done(function(data) {
console.log(data);
});