2016-05-31 84 views
-1

我花了一段时间试图使基本代码能够使用Facebook Graph API。我认为问题在于,当我单击按钮时,该操作从未启动。我试图在document.getElementById('login').onclick = function() {中添加警报,但它永远无法完成。你有什么想法可能是什么问题?Facebook Graph API不起作用

谢谢!

下面是代码( '0000000亿' 是不是我真正的应用程序ID,我只是取代它的职位):

<html> 
 
<head> 
 
</head> 
 
<body> 
 
<script type="text/javascript"> 
 
    window.fbAsyncInit = function() { 
 
     var appID = '0000000000000000'; 
 
     FB.init({ 
 
       appId: appID, 
 
       status: true, 
 
       cookie: true, 
 
       xfbml: true 
 
     }); 
 

 
     // This bit adds the login functionality to the login 
 
     // button when api load is complete 
 

 
     document.getElementById('login').onclick = function() { 
 
\t \t alert("test"); 
 
     FB.login(function(response) { 
 
     if (response.authResponse) { 
 
      FB.api('/me', function(info) { 
 
       login(response, info); 
 
      }); \t  
 
      } else { 
 
       //user cancelled login or did not grant authorization 
 
       showLoader(false); 
 
      } 
 
     }, {perms:'read_stream,publish_stream,offline_access'}); 
 
    } 
 
    }; 
 
    (function() { 
 
     var e = document.createElement("script"); 
 
     e.async = true; 
 
     e.src = "https://connect.facebook.net/en_US/all.js?xfbml=1"; 
 
     document.getElementById("fb-root").appendChild(e); 
 
    }()); 
 
</script> 
 
<button id="login" >My login button</button> 
 
</body> 
 
</html>

回答

0

这将是正确的代码:

document.getElementById('login').addEventListener('click', function() { 
    console.log('test'); 
}); 

的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

+0

ŧ为您的快速回答。我认为这是与Firefox一起使用的,不是吗?我正在使用铬,但它也没有工作。 我试图根据您的要求更改该行,但没有任何反应...... – Raquel

+0

这与浏览器无关。你检查了浏览器控制台吗?只有“它不起作用”很难提供帮助。调试你的东西,并阅读我发布的链接中的信息。正如你所看到的,即使在chrome 1.0中,它也能工作。 – luschn