Skip to content Skip to sidebar Skip to footer

How To Extract Url Data From Reddit API Using JSON

I'm trying to extract the image post URLs from a subreddit feed, and render elements on my page. Been trying to hack together the .getJSON() Flickr example from the jQu

Solution 1:

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");
    });
});

You should use data.data.children and not data.children


Solution 2:

For internet friends being lost here:

fetch("http://www.reddit.com/r/pics/.json")
  .then(r => r.json()).then((r) => {
     r.data.children.forEach((i) => {
       try{
         document.body.appendChild(Object.assign(document.createElement("img"),{src: i.data.thumbnail}))
       } catch (error){console.log(error.message)}
 })})

Post a Comment for "How To Extract Url Data From Reddit API Using JSON"