Facebook Javascript SDK: Processing Response from Facebook

孤街浪徒 提交于 2019-12-11 04:58:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!