speedracer2003
speedracer2003

Reputation: 171

How to access json content from external url

I'm new to jquery and javascript and I've been trying to access the contents of a json file from an external link with no luck. The json file is produced in the link below.

http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x

I noticed that several examples have a url similar to this "www.samplesite.com/testfile.json" However, as you can see above, the URL is not like this. Opening the link in Chrome takes you directly to the json file contents, however opening the file in IE asks you if you want to save the file "A10,A11.json".

All I want to do is be able to display the json file content in HTML. Can some please show me a brief example.

Thank you

Upvotes: 2

Views: 3597

Answers (4)

aolsen
aolsen

Reputation: 36

To retrieve JSON from servers outside your own domain, you should set up the callback to retrieve so called 'padded' JSON, which is easily done by adding the following to the jQuery .ajax() function:

 dataType: 'jsonp'

Upvotes: 0

bobbyg603
bobbyg603

Reputation: 3840

$.getJSON('http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x', function(data) {
    //data is the JSON string
});

Upvotes: 0

evilunix
evilunix

Reputation: 960

$.ajax({
  url: 'http://api.wmata.com/StationPrediction.svc/json/GetPrediction/A10,A11?api_key=hadtcpbh3w5xjbtyqrzgm88x',
  dataType: 'jsonp',
  success: function(data){// your code here
  }
});

Upvotes: 2

user3991493
user3991493

Reputation:

You can access the JSON only if the website having the JSON allows cross origin resource sharing(CORS). Find out if that is available, and if it is then post your code.

Upvotes: 0

Related Questions