问题
I'm having trouble understanding how to process the response when you make an API call to facebook. Should mention that I'm new to javascript.
Say I want to get the mutual friends between me and some user, and then write it to a text box. I took a shot and ended up with this ...which failed. I'm pretty sure it's because I didn't parse the response, but I can't figure out how. I've seen something like query.wait(function(rows){
for FQL queries ... but I don't know what that means..so I skipped it.
function getMutual(){
FB.api('/me/mutualfriends/'+target, function(response) {
document.getElementById('debug').innerHTML =
'FQL Information: '+ response +"<br />";
});
}
回答1:
In the beginning of your callback function function(response) {
, do either a debugger;
statement to start debugging in your browser, or you can do a console.dir(response);
to see the object structure. Half your battle learning javascript is learning how to use some powerful tools that modern browsers now have built in.
回答2:
The response will return the following data:
{
"data": [
{
"name": "XXX",
"id": "YYY"
},
{
"name": "ZZZ",
"id": "OOO"
},
...
}
To get the information by JS, you can iterate the array response.data
for example response.data[0].name return XXX
来源:https://stackoverflow.com/questions/9122668/facebook-javascript-sdk-processing-response-from-facebook