javascript facebook API 如何调用 onlogin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11771534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:17:19  来源:igfitidea点击:

facebook API how is onlogin called

javascripthtmlfacebookfacebook-graph-apifacebook-javascript-sdk

提问by The Learner

Can any one tell me how exactly the FB API works. It looks to be a basic question but I am really confused.

谁能告诉我 FB API 究竟是如何工作的。这看起来是一个基本问题,但我真的很困惑。

Question: I have onlogin(). When I click the login button, I am expecting it to call this function. But in the code I pasted: I see that alert-test is printed first and than the FB.api is called.

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

So, it looks like onlogin is called first, then the FB API ...is there a way I can call this function only once.

所以,看起来先调用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

My main issue is the function should be called only once....but it is getting called twice.

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

采纳答案by Purusottam Kaushik

    <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>

回答by Purusottam Kaushik

Request call to FB API is asynchronous. It means that once the request is sent, the code will not wait for the request to be completed. Because of this you get alert-test before the API Call is returned. All the FB API calls are asynchronous, if you try to make them synchronous then your browser will hang.

对 FB API 的请求调用是异步的。这意味着一旦请求被发送,代码将不会等待请求完成。因此,您会在 API 调用返回之前获得警报测试。所有 FB API 调用都是异步的,如果您尝试使它们同步,那么您的浏览器将挂起。

Thanks Kaushik

谢谢考希克