2013-02-22 183 views
0

我正在嘗試將Facebook登錄與我的網站進行整合,並且我現在正在通過本地主機測試我的Facebook登錄。這是我根據Facebook JavaScript SDK指南中的說明生成的代碼。Facebook JS SDK無法加載資源

<html> 
<body> 
<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '*************', // App ID 
     channelUrl : 'http://localhost', // 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') { 
    // connected 
    testAPI(); 
    } 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 
      console.log('Login failed!'); 
     } 
    }); 

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

    (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> 
<script src="//connect.facebook.net/en_US/all.js"></script> 
</body> 
</html> 

它說,它無法加載資源:file://connect.facebook.net/en_US/all.js它基本上是失敗的GET all.js.怎麼了?

我的Facebook應用程序儀表板有localhost作爲AppDomain和網站URL爲http://localhost

回答

0

請嘗試下面的代碼。

(function(){ 
var e = document.createElement('script'); 
e.type = 'text/javascript'; 
e.src = 'http://connect.facebook.net/en_US/all.js'; 
e.async = true; 
document.getElementById('fb-root').appendChild(e); 
}()); 
+0

我是否應該用我的代碼替換部分代碼?或者我只是將它插入某處? – dtgee 2013-02-22 07:42:29

+0

[Blog](http://webuntudro.com/connect-with-facebook/) – Sadik 2013-02-22 07:52:45

0

2018更新答:

1)file://它需要在一個web服務器加載測試HTML文件無法加載(如:http://localhost/test.html

2)禁用腳本/廣告攔截器。他們中的大多數人將FB SDK視爲「邪惡」並阻止它。