2013-03-27 30 views
17

我想從Facebook JS SDK的文檔運行第一個示例。我創建了一個新應用程序,創建了一個名爲「facebookTest.html」的空白文檔,粘貼了示例中的代碼,並插入了新應用程序的應用程序ID。代碼如下:使用Facebook的示例代碼「無法加載資源」

<html> 
<head> 
<title>Login with facebook</title> 
<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    // init the FB JS SDK 
    FB.init({ 
     appId  : 'my app ID', // App ID from the App Dashboard 
     status  : true, // check the login status upon init? 
     cookie  : true, // set sessions cookies to allow your server to access the session? 
     xfbml  : true // parse XFBML tags on this page? 
    }); 

    // Additional initialization code such as adding Event Listeners goes here 

    }; 

    // Load the SDK's source Asynchronously 
    // Note that the debug version is being actively developed and might 
    // contain some type checks that are overly strict. 
    // Please report such bugs using the bugs tool. 
    (function(d, debug){ 
    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 = document.location.protocol+"//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js"; 
    ref.parentNode.insertBefore(js, ref); 
    }(document, /*debug*/ false)); 
</script> 

當網頁加載完畢後,這個Javascript控制檯,我得到了以下錯誤消息上:

Failed to load resource

我在做什麼錯?

編輯:當我"//connect.facebook.net/..."前加document.location.protocol,如建議here,屏幕一直空白,並在控制檯顯示以下內容:

GET file://connect.facebook.net/en_US/all.js 

是:所有這些代碼是應該做的?還是它仍然失敗?

+0

檢查您的廣告攔截! :) – FooBar

回答

37

有什麼錯在這裏有兩件事情:

  1. 需要改變js.src分配: js.src="https://connect.facebook.net/en_US/all.js";

  2. 的Facebook不再支持從本地文件所做的JS SDK調用,劇本有根據this bug report on Facebook,在具有http://https:// URI的文件上運行。我需要將文件上傳到Web服務器,相應地更改畫布URL,然後重新測試。

+2

對指針2的解釋。這是因爲init代碼中的'js.src'是協議相對的。所以如果你正在訪問本地文件。它會嘗試訪問文件fb sdk:和錯誤 –

+0

是的,我發現我可以在本地Web服務器(例如WAMP)上測試它,如果我訪問該文件,例如'localhost/FacebookApp'而不是'file :// C:\ WAMP \ WWW \ FacebookApp \ index.html'。 – sigil

+1

是的。這是因爲你的js.src可能指向''//connect.facebok ....'它是協議相對的,因此,如果你在本地沒有服務器訪問你的頁面,例如使用'file://',那麼facebook init試圖從'file://connect.facebook.net ....'加載javascript sdk,這是不會發生的。所以它不起作用。 另外..最好有js.src指向'// connect.facebook.net/en_US/all.js',以便處理http請求。無論如何..我今天知道了,所以想分享。 –

5

首先,你需要追加HTTP或HTTPS來js.src網址

其次您需要託管在服務器上的HTML文件。

0

轉到Facebook開發人員,並添加您的網站域名,並獲得ID和粘貼如下:

appId : 'my app ID', // App ID from the App Dashboard 
0

只是禁用Adblock的, 工作對我來說