2012-04-26 3 views
2

로그인 한 후 사용자가 Facebook 페이지의 팬이 아니지만 결과가 항상 "정의되지 않음"인 경우 반환하려고합니다. 그러나 "복귀"를 "경고"로 대체하면 완벽하게 작동합니다.사용자가 Facebook 페이지의 팬인지 확인합니다.

function pageFan() 
{ 
    FB.api({ method: 'pages.isFan', page_id: '175625039138809' }, function(response) { 
     showAlert(response); 
    }); 
} 

function showAlert(response) 
{ 
    if (response == true) { 
     return 'like the Application.'; 
    } else { 
     return "doesn't like the Application."; 
    } 
} 

var like = pageFan(); 
document.getElementById('debug').innerHTML = like; //return undefined 

답변

1

이 질문은 이미 been answered입니다.

관련 자바 스크립트 :

showAlert에서 returnpageFan 기능 "에"반환되지 않기 때문이다
$(document).ready(function(){ 
     FB.login(function(response) { 
     if (response.session) { 

      var user_id = response.session.uid; 
      var page_id = "40796308305"; //coca cola 
      var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id; 
      var the_query = FB.Data.query(fql_query); 

      the_query.wait(function(rows) { 

       if (rows.length == 1 && rows[0].uid == user_id) { 
        $("#container_like").show(); 

        //here you could also do some ajax and get the content for a "liker" instead of simply showing a hidden div in the page. 

       } else { 
        $("#container_notlike").show(); 
        //and here you could get the content for a non liker in ajax... 
       } 
      }); 


     } else { 
     // user is not logged in 
     } 
    }); 
+0

response.session은 구형입니다. 올바른 방법은 다음과 같습니다. response.authResponse – Philip

0

. showAlert 함수는 콜백으로 전달됩니다. 즉, pageFan의 실행 범위 밖에서 나중에 호출됩니다. 나는 more about callback functions and asynchronous programming을 읽을 필요가 있다고 생각합니다.

function showAlert(response) 
{ 
    if (response == true) { 
     document.getElementById('debug').innerHTML = 'like the Application.'; 
    } else { 
     document.getElementById('debug').innerHTML = "doesn't like the Application."; 
    } 
} 
+0

그래,하지만 해결책은 무엇입니까? –

+0

해결책은 showAlert 내에서'debug' 요소를 수정하는 것입니다. 수정 된 답변 –

관련 문제