views:

168

answers:

3

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

A: 

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.

Hugo
Did that already. When I do an alert response it just says it's an object.
CoffeeAddict
actually it's undefined which makes sense cause I'm still getting back no data. The problem still remains even after the user has granted me the permission set I need to get at their albums and photos.
CoffeeAddict
+1  A: 

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.

sibtx13
If that's the case then why can't I do alert(response[0]); to see that string? when I do this, I just get 'object' as the value. So is the JSON string the only object in the response?
CoffeeAddict
Looks like I'm getting no data back: { data=}
CoffeeAddict
A: 

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.

daaku
well it's supposed to return a JSON object. Problem is no matter how many times I try to alert something out whether I try response[0] or whatever it may be I just keep getting "object" in the alert box. I don't know how to grab the JSON string so I can parse through it using jQuery's getJSON. I just need the string. But I don't understand why in their JS SDK examples they are showing response.name, etc. is that a php parsing thing that enables them to straight up reference the name of the value they're trying to work with?
CoffeeAddict
When I alert out the reponse[0] I get an undefined error but I know the response is not null
CoffeeAddict
Ok console.log says response is undefined. But if that's the case then why did it enter my callback and pass this check: if (!response || response.error) { alert("error occured"); return; }
CoffeeAddict
I did notice in firebug in the console this: Object { data=} so maybe there is a response string coming back but no data?
CoffeeAddict
updated the question with a code example.
CoffeeAddict
It's likely you don't have the required [permissions](http://developers.facebook.com/docs/authentication/permissions) to access albums. Here's an example: http://fbrell.com/fb.api/photo-albums.
daaku
Yea, I switched up to using the FB.login and rolling with my own button. Added the request for permissions. I was wondering though instead of sending /me/ I should be able to send the userid /userid/albums?access_token=..." as the uri correct/. Or don't I need to specify the access_token because the SDK can pick that up from the cookie automatically?
CoffeeAddict
this fbrell is nice but I don't get who made this and where the examples are stemming from. I mean I can log in but where is this code that's there coming from and how would I be able to see other types of syntax using the API like this? how would one know to type in photo-albums, what is this site?
CoffeeAddict