2012-07-26 73 views
0

我正在使用Facebook API来检查访问我的网页的用户是否已登录Facebook。我尝试了一切,无法提出解决方案。我想特别说明沙箱模式关闭。我的代码就在下面。即使沙盒设置关闭,FB.getLoginStatus也不会触发

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

    function fbLoginStatus(response) { 
     console.log("Status: "+response.session); 
     if(response.session) { 
      //user is logged in, display profile div 
      alert("Logged in"); 
     } else { 
      //user is not logged in, display guest div 
      alert("Not logged in"); 
     } 
    } 
     window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : '111111111111111', // Channel File 
      status  : true, // check login status 
      cookie  : true, // enable cookies to allow the server to access the session 
      xfbml  : true // parse XFBML 
     }); 
     FB.getLoginStatus(function(response){ 
      console.log("Hey"); 
      if(response.session){ 
       console.log("You are logged in"); 
      } 
      else{ 
       console.log("You are not logged in"); 
      } 
     }); 
     // Additional initialization code here 
     }; 
     // Load the SDK Asynchronously 
     (function(d){ 
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
     if (d.getElementById(id)) {return;} 

     js = d.createElement('script'); js.id = id; js.async = true; 
      js.src = "http://connect.facebook.net/en_US/all.js"; 
      ref.parentNode.insertBefore(js, ref); 
     }(document)); 

    </script> 

回答

1

FB.getLoginStatus是用来检测用户是否登录到FB的功能。在函数回调中,您正在检查response.session,但根据我的理解,此会话不是返回JSON对象的有效属性。您应该使用response.status。所以代码将变成如下所示。

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
    alert("LOG IN"); 
    } else if (response.status === 'not_authorized') { 
    // the user is logged in to Facebook, 
    // but has not authenticated your app 
    } else { 
    // the user isn't logged in to Facebook. 
    } 
}); 

希望这有助于

考希克

相关问题