简体   繁体   中英

Facebook Graph API does not work

I spent a while trying to make a basic code working to be able to use the Facebook Graph API . I think the issue is that the action is never launched when I click the button. I tried to add an alert in document.getElementById('login').onclick = function() { but it never goes through. Do you have any idea what could be the issue ?

Thanks !!

Here is the code ('000000000000000' is not my real app ID, I just replaced it for the post):

 <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() { alert("test"); FB.login(function(response) { if (response.authResponse) { FB.api('/me', function(info) { login(response, info); }); } 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>

That would be the correct code:

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

More information: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM