Iframe“(站点名称)拒绝连接。”错误

新手上路,请多包涵

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

TR : Projemde kullandığım iframe öğesinin içeriği “(站点广告)bağlanmayı reddetti。” Hatası alıyorum, bu hatayı nasıl düzeltebilim?

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

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

原文由 DevFatih 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

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

这是因为根据 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 的回答 所指出的,这可以用 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 模块。

原文由 scoochy 发布,翻译遵循 CC BY-SA 4.0 许可协议

几个月来我一直试图解决这个问题,最后找到了一个 非常 简单的解决方案,我还没有看到有人提到过。如果您尝试使用 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’。

原文由 Dale 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏