Chintan
Chintan

Reputation: 2808

How to fetch facebook json result by javascript?

I have one problem with facebook data fetching. The problem is I got the json array from facebook but I dont know how to fetch it by java script ! Insort I dont know how to fetch json array of facebook through java script !

Resultant data format is like

[

    {
        "uid": 100003596013776,
        "username": null,
        "name": "Milracle BE",
        "first_name": "Milracle",
        "last_name": "BE"
    }

]

I got this result from the facebook api methods !

Now the actual point is how to fetch this json data via java script ?

Upvotes: 2

Views: 2999

Answers (2)

DMCS
DMCS

Reputation: 31880

You can use the Facebook Javascript SDK as it returns the objects (based upon the JSON response) from Graph API calls.

See: http://developers.facebook.com/docs/reference/javascript/ and for doing a call to the graph's /me use http://developers.facebook.com/docs/reference/javascript/FB.api/

Using the SDK, the code will be easier to read and have less overhead than if you coded up everything yourself by hand.

FB.api('/me', function(response) {
  alert(response.name);
});

FB.api('/me/feed', { limit: 3 }, function(response) {
  for (var i=0, l=response.length; i<l; i++) {
    var post = response[i];
    if (post.message) {
      alert('Message: ' + post.message);
    } else if (post.attachment && post.attachment.name) {
      alert('Attachment: ' + post.attachment.name);
    }
  }
});

Upvotes: 1

buley
buley

Reputation: 29308

To speak with Facebook you'll need to use oAuth to have the user grant you what's called "access token."

From there, it's as simple as (for example, using jQuery):

jQuery.ajax( {
    url: 'http://graph.facebook.com/me'
    , dataType: 'json'
    , success: function( data ) { console.log( 'success', data ); }
    , error: function( data ) { console.log( 'error', data ); }
} );

Upvotes: 1

Related Questions