javascript 使用 Facebook 的示例代码“加载资源失败”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15669167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Failed to load resource" using Facebook's example code
提问by sigil
I'm trying to run the first example from the documentation of the Facebook JS SDK. I created a new app, created a blank document called "facebookTest.html", pasted in the code from the example, and plugged in the new app's App ID. Code as follows:
我正在尝试运行 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>
When I load the page, on the Javascript console I get the following error message:
当我加载页面时,在 Javascript 控制台上,我收到以下错误消息:
Failed to load resource
Failed to load resource
What am I doing wrong?
我究竟做错了什么?
EDIT: When I add document.location.protocol
before the "//connect.facebook.net/..."
, as suggested here, the screen stays blank, and the console shows the following:
编辑:当我按照此处的建议在document.location.protocol
之前添加时,屏幕保持空白,控制台显示以下内容:"//connect.facebook.net/..."
GET file://connect.facebook.net/en_US/all.js
Is that all this code is supposed to do? Or is it still failing?
这就是所有这些代码应该做的吗?或者它仍然失败?
回答by sigil
There were a couple things wrong here:
这里有一些错误:
needed to change
js.src
assignment to:js.src="https://connect.facebook.net/en_US/all.js";
Facebook no longer supports JS SDK calls made from a local file, the script has to be run on a file with an
http://
orhttps://
URI, as per this bug report on Facebook. I will need to upload the file to a web server, change the canvas URL accordingly, and retest.
需要将
js.src
分配更改为:js.src="https://connect.facebook.net/en_US/all.js";
Facebook 不再支持从本地文件调用 JS SDK,脚本必须在带有
http://
或https://
URI的文件上运行,根据Facebook 上的这个错误报告。我需要将文件上传到 Web 服务器,相应地更改画布 URL,然后重新测试。
回答by yawa yawa
just disable adblock, it works for me
只需禁用广告拦截,它对我有用
回答by user3059993
First you need to append http or https to js.src url
首先,您需要将 http 或 https 附加到 js.src url
Second you need to host you html file on a server.
其次,您需要在服务器上托管您的 html 文件。
回答by Developerjas
Go To Facebook developer and add your website domain and get the id and paste below:
转到 Facebook developer 并添加您的网站域并获取 id 并粘贴到下面:
appId : 'my app ID', // App ID from the App Dashboard
回答by Ashish Chaturvedi
If your URL contains words such as "advert", "ad", "doubleclick", "click", or something similar…
如果您的 URL 包含诸如“advert”、“ad”、“doubleclick”、“click”或类似的词……
For example:
例如:
GET googleads.g.doubleclick.net/pagead/id
static.doubleclick.net/instream/ad_status.js
GET googleads.g.doubleclick.net/pagead/id
static.doubleclick.net/instream/ad_status.js
…Then ad-blocker will block it.
...然后广告拦截器将阻止它。
Ref : Getting "net::ERR_BLOCKED_BY_CLIENT" error on some AJAX calls
回答by Reza
Adding one more thing I didn't see here: if you are debugging in Firefox, disable Enhanced Tracking Protection and Facebook Container while you are developing.
添加我在这里没有看到的另一件事:如果您在 Firefox 中进行调试,请在开发时禁用增强跟踪保护和 Facebook 容器。