【问题标题】:Iframe "(Site Name) Refused to Connect." Erroriframe“(站点名称)拒绝连接。”错误
【发布时间】:2020-10-26 18:31:28
【问题描述】:

EN : 我在项目中使用的 iframe 元素的内容是“(站点名称)拒绝连接。”我收到错误消息,如何解决此错误?

TR : Projemde kullandığım iframe öğesinin içeriği "(site adı) bağlanmayı reddetti." Hatası alıyorum, bu hatayı nasıl düzeltebilirim?

<iframe src="https://www.google.com/" frameborder="0"></iframe>

https://jsfiddle.net/fatihege/2he1nuyf/

【问题讨论】:

  • 网站可以阻止自己被陷害。你无能为力。将其更改为example.com,它将加载。

标签: html google-chrome iframe


【解决方案1】:

这不是你的代码,因为网站可以阻止自己被陷害。

这是因为根据 mozilla 文档设置了特定的标头(X-Frame-Options):https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>
<body>
  <iframe src="https://www.google.com/" frameborder="0"></iframe>
</body>
</html>

但使用另一个域(example.com):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>
<body>
  <iframe src="https://www.example.com/" frameborder="0"></iframe>
</body>
</html>

在不允许您查看该网站的情况下,您可以依靠服务器端来执行此操作。基本上这个概念是服务器将获取文件并将其附加到给定的位置。

正如@Dale's answer 所指出的,这可以通过php 来完成。 对于那些使用其他服务器端语言的人,基本上适用相同的原则。 这个在节点中的实现将是这样的:

const express = require('express');
const fs = require('fs');
const { execSync } = require('child_process');
const app = new express();
const PORT = 3000;

const fetchWebsite = (url) => {
  execSync(`wget -q -O - ${url} > site.html`,
    (error, stdout, stderr) => {
      if (error !== null) {
        return false;
      }
  });
}

app.get('/', async (req, res) => {
  fs.writeFileSync('site.html', '', () => console.log('Created site.html'));
  fs.createReadStream('site.html').pipe(res);
  fetchWebsite('https://www.stackoverflow.com');
});

app.listen(PORT, () => {console.log("Listening at port: ", PORT)} )

当然,这假设您使用的是标准 Linux 服务器并且您已经安装了 express 模块。

【讨论】:

    【解决方案2】:

    几个月来我一直在努力解决这个问题,终于找到了一个非常简单的解决方案,我还没有看到有人提到过。如果您尝试使用 iframe 来包含不允许您访问的网站,请创建您自己的 php 文件,我将其命名为 iframe.php。在该文件中,输入以下内容:

    <!DOCTYPE html><html><?php echo file_get_contents($_REQUEST['url']); ?></html>
    

    然后,在您的 iframe 中,使用 src url 'iframe.php?url=http://www.website.com'。

    【讨论】:

    • 这是正确答案。将此文件放在网络上的任何位置(可以使用 php),您就可以从任何地方(例如在 Jekyll 展示演示文稿中)查询 iframe 中的任何站点。用hackoverflow测试。谢谢!
    • stackoverflow.com/a/16625076/10630142 于 2013 年回答,似乎仍然有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多