最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Importing a JSON feed from an external source in Javascript - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

jQuery 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/

发布评论

评论列表(0)

  1. 暂无评论