Stefan P
Stefan P

Reputation: 1043

Jquery ajax with facebook social graph json

I'm trying to create a feed using jquery ajax and requesting data from facebooks social graph using json method.

Can someone please tell me why it isn't working? i've tried $.getJSON(); to no luck!

This is the code I have just tested and still no luck:

 $(document).ready(function(){ 


    var url = "http://graph.facebook.com/prettyklicks/feed?limit=3&callback=?pk";

        $.getJSON(url,function(json){

        var html = "<ul>";

    $.each(json.data,function(i,fb){

        html += "<li>" + fb.message + "</li>"; 

    });

    html += "</ul>";

    $('.facebookfeed').html(html);

});

});

Upvotes: 1

Views: 4337

Answers (2)

Jared Hales
Jared Hales

Reputation: 349

You are close, just remove the 'pk' from the json url, let jquery provide the name of the callback for you:

$(document).ready(function(){ 
  var url = "http://graph.facebook.com/prettyklicks/feed?limit=3&callback=?";
  $.getJSON(url,function(json){
    var html = "<ul>";
    $.each(json.data,function(i,fb){
      html += "<li>" + fb.message + "</li>"; 
    });
    html += "</ul>";
    $('.facebookfeed').html(html);
  });
});

sAc is right with his answer, you do need to use JSON-P. But jQuery will automatically take care of that for you if you pull JSON from a different domain.

Upvotes: 3

Sarfraz
Sarfraz

Reputation: 382616

You cannot get the data from different domain because of Same Origin Policy using Ajax. However, have a look at JSONP to get around it.

Upvotes: 0

Related Questions