I’m trying to extract the image post URLs from a subreddit feed, and render <img>
elements on my page.
Been trying to hack together the .getJSON()
Flickr example from the jQuery Docs for a while now and I’m not getting anywhere.
Code in question:
JavaScript
x
6
1
$.getJSON('http://www.reddit.com/r/pics.json', function (data) {
2
$.each(data.children, function (i, item) {
3
$('<img/>').attr("src", url).appendTo("#images");
4
});
5
});
6
In the body, I have the element: div#images
I understand that I need to use JSONP, but not sure how. Can somebody point me in the right direction?
Advertisement
Answer
You are using the wrong url. Use this:
JavaScript
1
4
1
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
2
// Do whatever you want with it..
3
});
4
EDIT : Working example based on your fiddle in the comments.
JavaScript
1
6
1
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
2
$.each(data.data.children, function(i,item){
3
$("<img/>").attr("src", item.data.url).appendTo("#images");
4
});
5
});
6
You should use data.data.children
and not data.children