Facebook 身份验证 - 不安全的 JavaScript 尝试使用 URL 访问框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8013008/
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
Facebook Authentication - Unsafe JavaScript attempt to access frame with URL
提问by I'll-Be-Back
I am trying to implement Facebook Login System into my website.
我正在尝试在我的网站中实施 Facebook 登录系统。
While it try to connect to facebook, I get an error from console log:
当它尝试连接到 facebook 时,我从控制台日志中收到错误消息:
Unsafe JavaScript attempt to access frame with URL https://s-static.ak.fbcdn.net/connect/xd_proxy.php?xxxxxxxxxxxxxxxx
I am using JavaScript SDK
我正在使用 JavaScript SDK
I added this in the body
tag:
我在body
标签中添加了这个:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxx',
status : true,
cookie : true,
xfbml : true
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
Facebook Login button:
脸书登录按钮:
<div class="fb-login-button" data-perms="email">Login with Facebook</div>
I am testing from localhost - I have register my website on facebook develop apps (Site URL: http://localhost
)
我从本地主机测试-我有注册自己的网站在Facebook上开发的应用程序(网站网址:http://localhost
)
How to fix this problem? or should I use PHP SDK?
如何解决这个问题?还是应该使用 PHP SDK?
Edit: Same problem when I uploaded to server with public domain.
编辑:当我上传到具有公共域的服务器时出现同样的问题。
回答by user1122069
The error that you see there is a non-fatal error. There is no workaround for this, as your browser is advising you that it is not a great idea to load JavaScript from foreign domains. Just ignore this message and look elsewhere for bugs if your scripts do not run. Sajith is also correct that you can't debug from localhost.
您看到的错误是非致命错误。对此没有解决方法,因为您的浏览器会建议您从外部域加载 JavaScript 并不是一个好主意。如果您的脚本没有运行,请忽略此消息并在别处查找错误。Sajith 也是正确的,您无法从本地主机进行调试。
See here also, "Unsafe JavaScript attempt to access frame with URL..." error being continuously generated in Chrome webkit inspector
另请参阅此处, “不安全的 JavaScript 尝试访问具有 URL 的框架...”错误在 Chrome webkit 检查器中不断生成
回答by OlivierG
Be sure to have this line on the top of your page (it works for me) :
确保在您的页面顶部有这一行(它对我有用):
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="https://www.facebook.com/2008/fbml">
回答by Sajith Amma
It is not possible to test all Facebook integration using domain localhost. You need to place your script in any public access domain and need to update the url in developer app settings page. The security error showing from JavaScript is due to this localhost access by facebook scripts
无法使用域 localhost 测试所有 Facebook 集成。您需要将脚本放在任何公共访问域中,并需要更新开发者应用设置页面中的 url。JavaScript 显示的安全错误是由于 facebook 脚本对本地主机的访问
回答by Anto
You can only use the same domain name in which you have registered with Facebook apps.
您只能使用在 Facebook 应用程序中注册的相同域名。
1. You need a public domain name (www.example.com). 2. You need to register with Facebook apps. 3. Get the 2 keys from the Facebook and use in the code:
appId : 'xxxxxxxxxxxxxx',
回答by technomage
You can tweak your localhost machine to appear to be of the domain that facebook requires.
您可以调整您的 localhost 机器,使其看起来属于 facebook 所需的域。
On linux or OSX, add an entry to /etc/hosts; on windows, edit c:\windows\system32\drivers\etc\hosts. Add an entry like this:
在 linux 或 OSX 上,向 /etc/hosts 添加一个条目;在 Windows 上,编辑 c:\windows\system32\drivers\etc\hosts。添加这样的条目:
127.0.0.1 local-dev.mydomain.com local-dev
Where "mydomain.com" is the domain with which your FB app ID is registered.
其中“mydomain.com”是注册您的 FB 应用 ID 的域。
回答by Alvin K.
Had this error when I implemented filegluand it may be a CORSerror since you are running off localhost. Try running on Facebook and can you replace the bottom half with the code below:
当我实现fileglu时出现此错误,这可能是CORS错误,因为您正在运行本地主机。尝试在 Facebook 上运行,你能不能用下面的代码替换下半部分:
(function() {
var e = document.createElement('script');
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js#appId=<your-app-id>&xfbml=1';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
I also had Oauth 2.0 and custom channel enabled in my FB.init() page. See: http://fbdevwiki.com/wiki/FB.initfor more information.
我还在 FB.init() 页面中启用了 Oauth 2.0 和自定义通道。请参阅:http: //fbdevwiki.com/wiki/FB.init了解更多信息。
回答by Dmitriy
In my case error was caused by like widget that was loaded using addthis on the site that already had FB app.
在我的情况下,错误是由在已经拥有 FB 应用程序的站点上使用 addthis 加载的类似小部件引起的。
It looked like problem was in conflicting with multiple fb js-sdk.
看起来问题与多个 fb js-sdk 冲突。
So solution could be replace addthis facebook like wdiget by native facebook like widget.
因此,解决方案可以将 addthis facebook like wdiget 替换为 native facebook like widget。
I've also had another bug in webkit when js-sdk was not loaded. I fixed that by replacing window.fbAsyncInit by jQuery.getScript(). Eventually it fixed error "Unsafe JavaScript attempt to access frame with URL" also.
当 js-sdk 未加载时,我在 webkit 中还有另一个错误。我通过用 jQuery.getScript() 替换 window.fbAsyncInit 来解决这个问题。最终它也修复了错误“不安全的 JavaScript 尝试使用 URL 访问框架”。
Here is example:
这是示例:
$.ajaxSetup({ cache: true });
$.getScript('//connect.facebook.net/en_UK/all.js', function() {
FB.init({
appId: 'your app id',
cookie: true,
xfbml: true,
oauth: true,
status: true
});
// Trigger custom event so we can use it to run init dependent actions in different places.
$(document).trigger('FBSDKLoaded');
//...
});