从Facebook登录按钮获得回复

时间:2012-01-28 07:44:46

标签: facebook-login

我在从facebook登录按钮获取回复时遇到问题。 并且非常感谢任何即时帮助。 我使用以下代码:

<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=MY_APP_ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1"></div>

<script type="text/javascript">
FB.Event.subscribe('auth.authResponseChange', function(response) {
    if(response.status=="connected")
    {
        document.getElementById("fblogin").value=response.authResponse.userID;
    }

});
</script>

...但我在javascript控制台中收到错误:“FB未定义”并且没有收到auth responeId的警告消息。

请帮助它非常重要

2 个答案:

答案 0 :(得分:1)

或者,如果您仍想加载JS SDK异步样式,可以在window.FbAsyncInit()中连接事件处理程序,如下所示:

  window.fbAsyncInit = function() {
      FB.init({
          appId      : 'YOUR_APP_ID', // App ID
          channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
      });

      // Additional initialization code here
      FB.Event.subscribe('auth.authResponseChange', function(response) {
          if(response.status=="connected")
          {
            document.getElementById("fblogin").value=response.authResponse.userID;
          }
      });

  };

<script>

(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0];

  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=MY_APP_ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1">
    </div>
<script type="text/javascript">

FB.Event.subscribe('auth.authResponseChange', function(response) {
    if(response.status=="connected")
    {
        document.getElementById("fblogin").value=response.authResponse.userID;
    }

});

</script>

您可以在此处看到此样式(这是Facebook首选的样式):https://developers.facebook.com/docs/reference/javascript/

答案 1 :(得分:-1)

您正在通过以下代码异步加载facebook javascript sdk

<div id="fb-root"></div> 
<script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0];

  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=MY_APP_ID";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

因此,在加载FB之前,不会定义它。

尝试同步加载FB。放下以下代码并删除上面的代码,

<script src="//connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({
    appId      : 'YOUR_APP_ID',
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });
</script>