2

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

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
4

2 回答 2

1

这个问题已经回答了

相关的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
      }
    });
于 2012-04-26T17:26:57.953 回答
0

那是因为returninshowAlert没有返回“进入”pageFan函数。该showAlert函数作为回调传递,这意味着稍后将在pageFan' 执行之外调用它。我认为您需要阅读有关回调函数和异步编程的更多信息。

function showAlert(response)
{
    if (response == true) {  
        document.getElementById('debug').innerHTML = 'like the Application.';
    } else {
        document.getElementById('debug').innerHTML = "doesn't like the Application.";
    }
}
于 2012-04-26T17:27:39.210 回答