0

我的 fb javascript sdk 代码按预期加载到我的索引页面上。在我的 js 脚本中,我有一个回调函数,一旦我拥有登录状态,我就想触发它。当我将回调与初始化代码一起包含在索引页面上时,回调有效,但当我将它移动到另一页上的脚本时无效。为什么?

//INDEX

 <div id="fb-root"></div>
 <script type="text/javascript">

     window.fbAsyncInit = function() {
    FB.init({
      appId      : '278xxxxx040', // App ID
      channelUrl : '//http://xxxxx/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
    });


     //get login status
         FB.getLoginStatus(onCheckLoginStatus(response));


  //JS

//OnCheckLoginStatus only works if I have it on the index page. 

function onCheckLoginStatus (response)
    {
     if (response.status != "connected")
     {
      top.location.href = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent (redirectUrl) + "&scope=user_photos,friends_photos";
     }
     else
     {
     console.log("connected"); 


      $.ajax({
        url : "http://sxxxxx/bn/s_Request.php",
        type : 'POST',
        data: {signed_request: response.authResponse.signedRequest},
        success : function (result) {
           console.log("success");
           console.log(result);
        },
        error : function () {
           alert("error parsing signed request");
        }
    });


      // Start the application (this is just demo code)!
      $(document.body).append ("<p>Authorized!</p>");

     }
    } 
4

1 回答 1

2

FB.getLoginStatus接受一个函数onCheckLoginStatus。只要FB.getLoginStatus在范围内找到引用就可以了。通过将函数移动到下一个文件,它超出了范围。

于 2013-07-26T20:22:38.460 回答