facebook API如何调用onlogin

The*_*ner 8 html javascript facebook facebook-graph-api facebook-javascript-sdk

任何人都可以告诉我FB API的工作原理.它看起来是一个基本问题,但我真的很困惑.

问题:我有onlogin().当我单击登录按钮时,我希望它能够调用此功能.但是在我粘贴的代码中:我看到首先打印警报测试,然后调用FB.api.

因此,看起来首先调用onlogin,然后调用FB API ...有一种方法我只能调用此函数一次.

<body>
        <div id="fb-root"></div>
<script>

      window.fbAsyncInit = function() {
          FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true});

      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());

    function checkFacebookLogin() {
    FB.api('/me', function(response) {
        alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id);
    });

    alert('test');

    }

    </script>


 <p id="fb_login_button_1"><fb:login-button  onlogin="checkFacebookLogin();"  size="medium" scope="user_about_me">Sign in using Facebook</fb:login-button></p>


</body>v
Run Code Online (Sandbox Code Playgroud)

我的主要问题是该函数应该只调用一次....但它被调用两次.

小智 7

    <body>
        <div id="fb-root"></div>
<script>

      window.fbAsyncInit = function() {
          FB.init({appId: 'XXX', status: true, cookie: true, xfbml: true});

      };
      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());

    function fetchUserDetail()
    {
        FB.api('/me', function(response) {
                alert("Name: "+ response.name + "\nFirst name: "+ response.first_name + "ID: "+response.id);
            });
    }

    function checkFacebookLogin() 
    {
        FB.getLoginStatus(function(response) {
          if (response.status === 'connected') {
            fetchUserDetail();
          } 
          else 
          {
            initiateFBLogin();
          }
         });
    }

    function initiateFBLogin()
    {
        FB.login(function(response) {
           fetchUserDetail();
         });
    }
    </script>


 <input type="button" value="Sign in using Facebook" onclick="checkFacebookLogin();"/>


</body>
Run Code Online (Sandbox Code Playgroud)