1

I am trying to load images from the Facebook graph api into a div on my website.

I have a div for each of the albums on my facebook page, each has the url id in the rel value. I am trying to loop through the graph api urls and load the first image in each json datablock using the code below:

$('.thumbnail').each(function(index) {
    $.getJSON('http://graph.facebook.com/'+ $(this).attr('rel') +'/photos', function(data) {
       $(this).css('background-image', 'url('+ data.data[0].picture +')');
    });
});

You can see an example of the json here: http://graph.facebook.com/422882642272/photos

I am just trying to traverse the json and load the 'picture' value from the first node.

2 Answers 2

1

demo

you missed the callback=? in the url...

$('.thumbnail').each(function(index) {
    var $this = $(this); // should save a reference of the current element...
    $.getJSON('http://graph.facebook.com/'+ $(this).attr('rel') +'/photos/?callback=?', function(data) {
       $this.css('background-image', 'url('+ data.data[0].picture +')');
    });
});​
Sign up to request clarification or add additional context in comments.

2 Comments

Please forgive my lack of programming skill, but although that demo appears to work (great!), I can't seem to work out how to translate it to my background image code?
Thank you so much. I would never have solved this one without your help. Have a great day! :)
0

Here is the short answer: You just cannot do that, due to same origina policy.

Long answer: Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

But I think the facebook API supports the JSONP callback with ?callback=xxx.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.