2013-07-26 2 views
0

my fb javascript sdk 코드가 내 인덱스 페이지에로드되어 있습니다. js 스크립트에서 로그인 상태가되면 콜백 함수를 사용하고 싶습니다. 콜백은 init 코드와 함께 인덱스 페이지에 포함 시키면 작동하지만 다른 페이지의 스크립트로 옮길 때는 작동하지 않습니다. 왜?fb.getLoginStatus()는 별도의 페이지에있는 스크립트에서 가지고있는 콜백 함수를 호출하지 않습니다.

//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>"); 

    } 
    } 

답변

2

FB.getLoginStatus 함수 onCheckLoginStatus을 받아 들인다. FB.getLoginStatus 범위 내에서 참조를 찾으면 작동합니다. 함수를 다음 파일로 이동하면 범위를 벗어납니다.

관련 문제