Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JSON从RedditAPI提取url数据_Javascript_Json_Api_Reddit - Fatal编程技术网

Javascript 如何使用JSON从RedditAPI提取url数据

Javascript 如何使用JSON从RedditAPI提取url数据,javascript,json,api,reddit,Javascript,Json,Api,Reddit,我正试图从subreddit提要中提取图像帖子URL,并呈现 在正文中,我有一个元素:div#images 我知道我需要使用JSONP,但不确定如何使用。有人能给我指出正确的方向吗?您使用的url错误。使用以下命令: $.getJSON('http://www.reddit.com/r/pics.json', function (data) { $.each(data.children, function (i, item) { $('<img/>').attr("src

我正试图从subreddit提要中提取图像帖子URL,并呈现

在正文中,我有一个元素:
div#images


我知道我需要使用JSONP,但不确定如何使用。有人能给我指出正确的方向吗?

您使用的url错误。使用以下命令:

$.getJSON('http://www.reddit.com/r/pics.json', function (data) {
  $.each(data.children, function (i, item) {
    $('<img/>').attr("src", url).appendTo("#images");
  });
});
编辑:基于您在评论中的评论的工作示例

$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) { 
    // Do whatever you want with it.. 
});
$.getJSON(“http://www.reddit.com/r/pics/.json?jsonp=?“,函数(数据){
$.each(data.data.children,函数(i,项){
$("

您应该使用
data.data.children
而不是
data.children

来表示在此处失去的网友:

fetch(“http://www.reddit.com/r/pics/.json")
。然后(r=>r.json())。然后((r)=>{
r、 data.children.forEach((i)=>{
试一试{
document.body.appendChild(Object.assign(document.createElement(“img”),{src:i.data.缩略图}))
}捕获(错误){console.log(error.message)}

})})
@ZachL我试过运行你的小提琴,但它也不起作用。我从数据变量中提取所需内容时没有遇到问题。我的函数甚至从未被调用过。@ZachL它似乎在Firefox中起作用,但在OSX 10.8.4上不起作用。@Talon876(问题是adblock。)
$.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");
    });
});