0

Facebook APIのFBオブジェクトは、初期化後にnullになるようです。これは私が使用しているコードで、非常に単純です。問題は、「/me」応答が未定義であると表示されることです。私はログインしていますが、再確認しただけなので、そうではありません。私はすでに自分のデータを使用する許可を与えました。

「/me」の応答が定義されていないのはなぜですか?

また、コードを移動する方法はありますか?

function testAPI() {
                        console.log('Welcome!  Fetching your information.... ');
                        FB.api('/me', function(response) {
                            console.log('Good to see you, ' + response.name + '.');
                        });
                    }

                    testAPI();

別の.jsファイル(コードが関数でラップされている)に入れ、関数がwindow.fbAsyncInit内で呼び出されたときにそれをロードしますか?私がやろうとしたすべての方法が失敗に終わった原因。

<!doctype html>  
<html lang="en">  
    <head>  
        <meta charset="utf-8">
        <title>FB-Stuff</title>
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                  appId      : '[my code]', // App ID
                  channelUrl : '//[my channel]', // Channel File
                  status     : true, // check login status
                  cookie     : true, // enable cookies to allow the server to access the session
                  xfbml      : true  // parse XFBML
                });

                function testAPI() {
                    console.log('Welcome!  Fetching your information.... ');
                    FB.api('/me', function(response) {
                        console.log('Good to see you, ' + response.name + '.');
                    });
                }

                testAPI();
            };

            // Load the SDK Asynchronously
            (function(d){
                var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                d.getElementsByTagName('head')[0].appendChild(js);
            }(document));
        </script>
    </body>
</html>

編集 - - - - - - - - - - - - - - - - - - - - - - - - - ------

現在、関数呼び出し/エラーメッセージはまったく表示されません。これは新しいコードです:

<!doctype html>  
<html lang="en">  
    <head>  
        <meta charset="utf-8">
        <title>FB-Stuff</title>
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="mycode.js"></script>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function() {
                FB.init({
                  appId      : 'xxx', // App ID
                  channelUrl : '//xxx/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
                });

                FB.getLoginStatus(function(response) {
                  if (response.status === 'connected') {
                        facebookInit();
                    }
                });
            };

            // Load the SDK Asynchronously
            (function(d){
                var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                d.getElementsByTagName('head')[0].appendChild(js);
            }(document));
        </script>
    </body>
</html>

そしてmycode.jsの中には:

function facebookInit(){
    function testAPI() {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.');
        });
    }
}
4

1 に答える 1

1

ステータスが解決されるFB.apiまで呼び出しを延期する必要がありますFB.getLoginStatus。このためのイベントの1つを使用するか、リッスンします。

コードを移動することの意味がわかりません。JSSDKの前にロードされている限り、このコードをどこにでも配置できます。

于 2012-12-08T18:54:50.183 に答える