2012-12-18 201 views
-1

我正在嘗試使用Facebook登錄幫助指南來組合腳本以檢查用戶是否已登錄。我已將以下內容放在一起,基於this guide意外的令牌錯誤

<div id="fb-root"></div> 
<script> 
    // Additional JS functions here 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '303594953085775', // App ID 
     channelUrl : 'http://mysite.com/pages/account/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 
    }); 

    // Additional init code here 
    FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
     // connected 
    } else if (response.status === 'not_authorized') { 
     // not_authorized 
     login(); 
    } else { 
     // not_logged_in 
     login(); 
    } 
    }); 
    }; 

    }; 

    function login() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
      // connected 
      testAPI(); 
     } else { 
      // cancelled 
     } 
    }); 
    } 

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

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

我完全不知道我做了什麼錯誤,因爲我完全複製了它。我將它複製了四次,儘管它正在正好是在Facebook網站上寫的內容,但仍然出現錯誤。

我得到兩個錯誤:

Uncaught SyntaxError: Unexpected token }

Uncaught SyntaxError: Unexpected token .

我真的不知道這種複雜的JavaScript的東西,因爲我只知道怎麼做了幾個基本通話費之類的東西,這正在使我想把自己從建築物上拋下。

誰能告訴我爲什麼它出錯? 是否有某處我可以掌握整個正確的腳本並替換我的應用程序ID和域名,而不必經過這個愚蠢的Facebook指南?

+1

他們未被捕獲SyntaxError消息也有他們的行和字符數?因爲它看起來像你有一個額外的};在第一個fbAsyncInit函數之後 – TommyBs

+0

你知道你可以用'if(response.status!=='''connected')login();' – ifaour

+0

替換你的'if-else'子句是你的真實應用ID嗎?不要在公共論壇上發佈。 –

回答

1

出現在你的代碼中存在語法錯誤:

window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : '303594953085775', // App ID 
    channelUrl : 'http://mysite.com/pages/account/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 
    }); 

    // Additional init code here 
    FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
     // connected 
    } else if (response.status === 'not_authorized') { 
     // not_authorized 
     login(); 
    } else { 
     // not_logged_in 
     login(); 
    } 
    }); 

}; 
};  **// REMOVE THIS- This one is extra** 

您可以使用記事本++或任何其他好的編輯器來避免此類錯誤:)

+0

謝謝,這將刪除一個錯誤,但其他仍然存在。 '。'在第14行顯然是非法的。http://pastebin.com/0UMTPdrp - 我得到Uncaught SyntaxError:意外的令牌。 - 困惑,因爲這是從Facebook直接採取的。他們自己的代碼是不正確的?! – Francesca

+1

@Francesca他們的代碼是正確的,你可以在philippeharewood.com/facebook/hello.html看到一個修改後的版本 – phwd

0

我具有通過複製而引起同樣的問題並直接粘貼代碼,從而以某種方式在其中放入一些奇怪的字符。我通過重寫它來修復它,而不是直接複製和粘貼。