【问题标题】:Check if a user Is fan of a Facebook page检查用户是否是 Facebook 页面的粉丝
【发布时间】:2012-04-26 17:20:23
【问题描述】:

登录后,如果用户不是 Facebook 页面的粉丝,我会尝试返回,但结果始终是“未定义”。但是如果我将“return”替换为“alert”效果很好。

function pageFan()
{
    FB.api({ method: 'pages.isFan', page_id: '175625039138809' }, function(response) {
        showAlert(response);
    });
}

function showAlert(response)
{
    if (response == true) {  
        return 'like the Application.';
    } else {
        return "doesn't like the Application.";
    }
}

var like = pageFan();
document.getElementById('debug').innerHTML = like; //return undefined

【问题讨论】:

    标签: javascript facebook api sdk


    【解决方案1】:

    这个问题已经been answered了。

    相关的Javascript:

    $(document).ready(function(){
          FB.login(function(response) {
          if (response.session) {
    
              var user_id = response.session.uid;
              var page_id = "40796308305"; //coca cola
              var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
              var the_query = FB.Data.query(fql_query);
    
              the_query.wait(function(rows) {
    
                  if (rows.length == 1 && rows[0].uid == user_id) {
                      $("#container_like").show();
    
                      //here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page.
    
                  } else {
                      $("#container_notlike").show();
                      //and here you could get the content for a non liker in ajax...
                  }
              });
    
    
          } else {
            // user is not logged in
          }
        });
    

    【讨论】:

    • response.session 已过时,正确的做法是:response.authResponse
    【解决方案2】:

    这是因为showAlert 中的return 没有返回“进入”pageFan 函数。 showAlert 函数作为回调传递,这意味着稍后将在 pageFan 的执行之外调用它。我认为你需要阅读more about callback functions and asynchronous programming

    function showAlert(response)
    {
        if (response == true) {  
            document.getElementById('debug').innerHTML = 'like the Application.';
        } else {
            document.getElementById('debug').innerHTML = "doesn't like the Application.";
        }
    }
    

    【讨论】:

    • 好的,我明白了,但是解决办法是什么?
    • 解决方法是修改showAlert中的debug元素。编辑答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多