PositiveGuy
PositiveGuy

Reputation: 47783

Facebook FB.api - just use the JSON object it sends back?

So taking a look at the API doc page here:

http://developers.facebook.com/docs/reference/javascript/FB.api

I'm wondering if the response received back is an HttpResponse or a JSON object. They say that they return a JSON object in the response.

So since they are performing things such as response.name, etc. does it mean we don't need to parse the JSON object? I don't get it. I was going to use the jQuery parseJSON to parse the returned JSON object so I could traverse through it and grab the data.

UPDATED:

Ok well here's my actual code:

var uri = "/" + userID + "/albums";

    FB.api(uri, function (response) {
        // check for a valid response
        if (!response || response.error)
        {
            alert("error occured");
            return;
        }


        alert("console.log(response): " + console.log(response));
        alert("response: " + response[0].length);
});

the uri being passed in is this: /1637262814/albums

Upvotes: 1

Views: 4317

Answers (3)

daaku
daaku

Reputation: 2807

You get a JavaScript value back. Graph API always returns an object, but some old methods return numbers or booleans. Usually this is an object like { name: 'My Name', id: 1234 }. Easiest to run this in firebug: FB.api('/me', function(r) { console.log(r) }) as it will let you explore the response. You can also take a look at this example: http://fbrell.com/fb.api/user-info.

Upvotes: 0

sibtx13
sibtx13

Reputation: 123

So they are just returning a JSON string, but your programming language wraps that into an HttpResponse. you have to extract the JSON string from the response and then parse it.

Upvotes: 1

WhyNotHugo
WhyNotHugo

Reputation: 9924

I don't mean to be impolite, but I think the fastest way to find this out would be to just try and alert(response), and/or alert(eval(response)) and see what happens.

Just alert(response) should be enough to let you know what you're getting and how to treat it.

Upvotes: 0

Related Questions