如何提取使用JSON API reddit的URL数据 [英] How to extract url data from Reddit API using JSON
问题描述
我想从影像只能版(Subreddit)中提取的URL,让他们内部的输出< IMG方式>
标签
I am trying to extract the URLs from an image-only subreddit and have them output inside <img>
tags.
一直试图破解一起 .getJSON()
的 Flickr的例子从jQuery文档即时了一段时间,我没有得到任何地方。
Been trying to hack together the .getJSON()
Flickr example from the jQuery Docs for a while now and I'm not getting anywhere.
下面是我的code:
$.getJSON("http://www.reddit.com/r/pics.json",
function(data) {
$.each(data.children, function(i,item){
$("<img/>").attr("src", url).appendTo("#images");
if ( i == 3 ) return false;
});
});
,并在&LT;身体GT;
,我有&LT; DIV ID =图片&GT;&LT; / DIV&GT;
据我了解,我需要使用JSONP,但我不知道该怎么做。有人能指出我朝着正确的方向吗?
I understand that I need to use JSONP, but I don't know how to do it. Can somebody point me in the right direction?
推荐答案
您使用了错误的URL。使用此:
You are using the wrong url. Use this:
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
// Do whatever you want with it..
});
编辑:在评论基于您的提琴 工作的例子
EDIT : Working example based on your fiddle in the comments.
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
$.each(data.data.children, function(i,item){
$("<img/>").attr("src", item.data.url).appendTo("#images");
});
});
您应该使用 data.data.children
,而不是 data.children
这篇关于如何提取使用JSON API reddit的URL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!