我正在写一个小网页,它的目的是框架其他一些页面,只是为了将它们合并到一个浏览器窗口中,以便于查看。一些页面,我试图框架禁止被框架,并抛出“拒绝显示文档,因为显示禁止X-Frame-Options.”错误在Chrome。我知道这是一个安全限制(有充分的理由),并且无法更改它。

是否有任何替代的框架或非框架方法来在单个窗口中显示页面,而不会被X-Frame-Options报头绊倒?


当前回答

<form target="_parent" ... />

根据Kevin Vella的想法,我尝试在PayPal的按钮生成器生成的表单元素上使用上述方法。为我工作,这样贝宝不会在一个新的浏览器窗口/标签打开。

更新

这里有一个例子:

在今天(01-19-2021)生成按钮时,PayPal自动在表单元素上包含target="_top",但如果这对您的上下文不起作用,请尝试不同的目标值。我建议_parent——至少当我使用这个PayPal按钮时,它是有效的。

有关更多信息,请参阅表单目标值。

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_parent">
  <input type="hidden" name="cmd" value="_xclick">
  <input type="hidden" name="business" value="name@email.com">
  <input type="hidden" name="lc" value="US">
  <input type="hidden" name="button_subtype" value="services">
  <input type="hidden" name="no_note" value="0">
  <input type="hidden" name="currency_code" value="USD">
  <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
  <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

其他回答

我有一个类似的问题,我试图在一个iframe中显示我们自己网站的内容(作为一个带有Colorbox的lightbox样式的对话框),并且我们在源服务器上有一个服务器范围的“X-Frame-Options SAMEORIGIN”报头,防止它加载到我们的测试服务器上。

这似乎没有被记录在任何地方,但如果你可以编辑你试图iframe的页面(例如。,它们是你自己的页面),简单地发送另一个X-Frame-Options报头,任何字符串都禁用SAMEORIGIN或DENY命令。

如。对于PHP,放入

<?php
    header('X-Frame-Options: GOFORIT'); 
?>

在页面的顶部会使浏览器将两者结合起来,从而导致页眉为

X-Frame-Options SAMEORIGIN, GOFORIT

...并允许您在iframe中加载页面。当初始的SAMEORIGIN命令设置在服务器级别时,这似乎可以工作,并且您希望在逐页情况下重写它。

祝你一切顺利!

唯一真正的答案,如果你不控制你想在iframe中的源的头,就是代理它。让服务器充当客户端,接收源,去除有问题的头,如果需要添加CORS,然后ping您自己的服务器。

还有一个解释如何编写这样一个代理的答案。这并不难,但我相信以前一定有人这么做过。只是因为某些原因,很难找到它。

我终于找到了一些资料来源:

https://github.com/Rob--W/cors-anywhere/#documentation

^者优先。如果你需要很少使用,我认为你可以使用他的heroku应用程序。否则,这是在你自己的服务器上运行它的代码。注意限制是什么。

whateverorigin.org

^第二选择,但相当古老。应该是python中的新选择:https://github.com/Eiledon/alloworigin

还有第三种选择:

http://anyorigin.com/

这似乎允许一些免费使用,但如果你不付钱,使用一些不确定的数量,你就会被列入公众的耻辱名单,只有在你付钱的情况下,你才能被删除……

有一个Chrome插件,删除头条目(仅供个人使用):

https://chrome.google.com/webstore/detail/ignore-x-frame-headers/gleekbfjekiniecknbkamfmkohkpodhe/reviews

将外部网站加载到iFrame的解决方案,即使外部网站的x帧选项设置为拒绝。

如果你想将其他网站加载到iFrame中,而你得到了X-Frame-Options禁止显示的错误,那么你实际上可以通过创建一个服务器端代理脚本来克服这个问题。

iFrame的src属性可以有这样一个url:/ proxy.php?url=https://www.example.com/page&key=somekey

那么proxy.php看起来就像这样:

if (isValidRequest()) {
   echo file_get_contents($_GET['url']);
}

function isValidRequest() {
    return $_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['key']) && 
    $_GET['key'] === 'somekey';
}

这通过传递块,因为它只是一个GET请求,可能是一个普通的浏览器页面访问。

注意:您可能需要改进此脚本中的安全性。因为黑客可以通过你的代理脚本开始加载网页。

I came across this issue when running a wordpress web site. I tried all sorts of things to fix it and wasn't sure how, ultimately the issue was because I was using DNS forwarding with masking, and the links to external sites were not being addressed properly. i.e. my site was hosted at http://123.456.789/index.html but was masked to run at http://somewebSite.com/index.html. When i entered http://123.456.789/index.html in the browser clicking on those same links resulted in no X-frame-origins issues in the JS console, but running http://somewebSite.com/index.html did. In order to properly mask you must add your host's DNS name servers to your domain service, i.e. godaddy.com should have name servers of example, ns1.digitalocean.com, ns2.digitalocean.com, ns3.digitalocean.com, if you were using digitalocean.com as your hosting service.