1
votes

I'm trying to get JSON data from facebook using the following code:

var getFriends = function() {
    var friendsURL = 'https://graph.facebook.com/<?= $cookie['uid'] ?>/friends?access_token=<?= $cookie['access_token'] ?>&jsoncallback=?';

    $.getJSON(friendsURL, function(data) {

        var names = "";

        $.each(data.name, function(){
            names += " " + this;
        });

        alert("JSON Data: " + names);
    });

}

Every time I run the function I get this error in Chrome: "Uncaught SyntaxError: Unexpected token :"

Any ideas? I made sure to put "jsoncallback=?" at the end of the url to make it return as JSONP but I've run out of solutions now.

Thanks, -Ben

2

2 Answers

3
votes

graph.facebook.com expects callback in the querystring, not jsoncallback, so your URL should look like this to trigger JSONP on their side (currently you're just getting JSON):

var friendsURL = 'https://graph.facebook.com/<?= $cookie['uid'] ?>/friends?access_token=<?= $cookie['access_token'] ?>&callback=?';
1
votes

Why not use the official Javascript SDK instead? That call would be:

FB.api('/userid/friends', function(response) {
  // response is an array of friends
});

The official library will handle the particulars for you so you don't have to worry about all the implementation details.