javascript 由于其“X-Frame-Options”而拒绝在框架中显示网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19137919/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:31:21  来源:igfitidea点击:

Refused to display a website in a frame because of its 'X-Frame-Options'

javascripthtmliframex-frame-options

提问by Ashraf Bashir

I'm trying to display Facebook page in an simple HTML page which only contains an iframe.

我试图在一个只包含 iframe 的简单 HTML 页面中显示 Facebook 页面。

Here's my HTML code:

这是我的 HTML 代码:

<html>
    <body>
        <iframe src="http://www.facebook.com"></iframe>
    </body>
</html>

I'm always getting this error in Google Chrome's console:

我总是在 Google Chrome 的控制台中收到此错误:

Refused to display 'http://www.facebook.com/' in a frame because it set 'X-Frame-Options' to 'DENY'.

拒绝在框架中显示“ http://www.facebook.com/”,因为它将“X-Frame-Options”设置为“DENY”。

BTW, I'm having this problem also with these iframes:

顺便说一句,我也遇到了这些 iframe 的问题:

<iframe src="https://mail.google.com/mail/mu/mp/any"></iframe>
<iframe src="http://m.facebook.com"></iframe>
<iframe src="http://m.youtube.com"></iframe>
<iframe src="http://m.dropbox.com"></iframe>
<iframe src="http://m.yahoo.com"></iframe>
<iframe src="http://www.google.com"></iframe>
<iframe src="http://mail.google.com"></iframe>
<iframe src="http://www.facebook.com"></iframe>
<iframe src="http://www.youtube.com"></iframe>
<iframe src="http://www.dropbox.com"></iframe>
<iframe src="http://www.yahoo.com"></iframe>

Meanwhile, this iframe is working well !

同时,这个 iframe 运行良好!

<iframe src="http://www.google.com/custom"></iframe>

How to solve this ?

如何解决这个问题?

EDIT based on comments:
Is there an alternative of using iframe if this is unsolvable ? Something like browsertag in XUL? I already tried browser tag in Firefox extension and it works well, but I'm now coding a chrome extension, so I cannot depend on XUL anymore ?

根据评论编辑:
如果无法解决,是否有使用 iframe 的替代方法?像browser标签一样的东西XUL?我已经在 Firefox 扩展中尝试过浏览器标签,它运行良好,但我现在正在编写一个 chrome 扩展,所以我不能再依赖 XUL 了?

采纳答案by Ashraf Bashir

For those who are interested, this cannot be done !

对于有兴趣的人来说,这是不可能的!

回答by Chloe

Yes, you could

是的,你可以

  • use server-side includes, to fetch it from the server and include it in an iframe
  • use a custom browser which does not recognize that header
  • complicated: use websockets to create your own Javascript HTTP client, fetch the page, and insert it into the DOM
  • 使用服务器端包含,从服务器获取它并将其包含在 iframe 中
  • 使用无法识别该标题的自定义浏览器
  • 复杂:使用 websockets 创建您自己的 Javascript HTTP 客户端,获取页面,并将其插入到 DOM 中