Importing a JSON feed from an external source into Javascript

I want to load a JSON feed from an external source using Javascript; what's the best method? I've worked a lot in PHP where it would be easy to do this with file_get_contents or cURL. Is there a related function or process in Javascript?

0


a source to share


2 answers


jQuery to get some JSON data might look like this:

$.getJSON("http://pathtodata.js", function(json){
  alert(json.dot.notation);
});

      



A source with a callback function is specified. Read in the JQuery JSON documentation: http://api.jquery.com/jQuery.getJSON/

+5


a source


Javascript XMLHTTPRequest has a single domain origin policy, so you'll only be limited to loading data from URLs from the same domain your script was loaded from. JSONP is one way to get around this. Another way is to use a proxy script on your domain that makes its own HTTP calls in turn for you. For more information on JSONP, check out this article:



http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

+4


a source







All Articles