"使用Facebook的示例代码加载资源失败"

sig*_*gil 26 javascript facebook facebook-javascript-sdk

我正在尝试从Facebook JS SDK的文档中运行第一个示例.我创建了一个新的应用程序,创建了一个名为"facebookTest.html"的空白文档,粘贴在示例的代码中,并插入新应用程序的App ID.代码如下:

<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>
Run Code Online (Sandbox Code Playgroud)

当我加载页面时,在Javascript控制台上,我收到以下错误消息:

Failed to load resource

我究竟做错了什么?

编辑:当我在此document.location.protocol之前添加时"//connect.facebook.net/...",如此处所示,屏幕保持空白,控制台显示以下内容:

GET file://connect.facebook.net/en_US/all.js  
Run Code Online (Sandbox Code Playgroud)

所有这些代码都应该这样做吗?还是它还在失败?

sig*_*gil 40

这里有一些错误:

  1. 需要将js.src作业更改为: js.src="https://connect.facebook.net/en_US/all.js";

  2. Facebook不再支持从本地文件发出的JS SDK调用,该脚本必须在具有http://https://URI 的文件上运行,根据Facebook上的此错误报告.我需要将文件上传到Web服务器,相应地更改画布URL,然后重新测试.

  • 在指针2的解释中.这是因为init代码中的`js.src`是协议相对的.所以,如果您正在访问本地文件.它将尝试访问fb sdk over file:和错误 (2认同)

小智 22

只是禁用adblock,它适用于我

  • 嘿,简单的最佳答案 =) (2认同)

use*_*993 6

首先,您需要将http或https附加到js.src网址

其次,你需要在服务器上托管你的html文件.