I'd like to load a JSON feed from an external source using Javascript; what's the best method? I've been working a lot in PHP where it would be easy to do so with file_get_contents or cURL. Is there a related function or process in Javascript?
I'd like to load a JSON feed from an external source using Javascript; what's the best method? I've been working a lot in PHP where it would be easy to do so with file_get_contents or cURL. Is there a related function or process in Javascript?
Share Improve this question edited May 26, 2009 at 22:27 Daniel Bachhuber asked May 26, 2009 at 21:50 Daniel BachhuberDaniel Bachhuber 7132 gold badges10 silver badges20 bronze badges2 Answers
Reset to default 5jQuery to get some JSON data might look like this:
$.getJSON("http://pathtodata.js", function(json){
alert(json.dot.notation);
});
A source is specified along with a callback function. Read up on jQuery JSON documentation: http://api.jquery./jQuery.getJSON/
Javascript XMLHTTPRequest has same-domain origin policy so you will be restricted to only loading data from URLs from the same domain that your script was loaded from. JSONP is one way to get around this. Another way is to use a proxy script on your domain, which in turns performs its own HTTP calls for you. For more information on JSONP check out this article:
http://www.ibm./developerworks/library/wa-aj-jsonp1/