facebook API如何调用onlogin [英] facebook API how is onlogin called

查看:122
本文介绍了facebook API如何调用onlogin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



问题:
我有onlogin()。当我点击登录按钮,我期望它调用这个功能。
但是在我粘贴的代码中:我看到alert-test首先打印,而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);
}());

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

alert('test');

}

< / script>


< p id =fb_login_button_1>< fb:login-button onlogin =checkFacebookLogin(); size =mediumscope =user_about_me>使用Facebook登录< / fb:login-button>< / p>


< / body> v

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

解决方案

 < 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);
}());

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

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

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


< input type =buttonvalue =使用Facebook登录onclick =checkFacebookLogin();/>


< / body>


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

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.

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

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

解决方案

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

这篇关于facebook API如何调用onlogin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆