Skip to content

DOMException: Blocked frame 使用原点从 accessing cross-原点 frame

错误信息

                Blocked frame 使用原点 "X" 从 accessing cross-原点 frame
              

快速摘要

你 tried 到 access iframe, window, or document 从不同原点 — 相同-原点 Policy 块这个 for security.

为什么发生

  • Tried 到 access iframe's 内容从 不同领域

  • Attempted 到读取属性的 window 打开到 another 原点

  • 制作获取请求到 服务器不使用 proper CORS 请求头

最小示例

✗ 错误代码
const iframe = document.getElementById("frame");
const data = iframe.contentWindow.document.body.innerHTML;
✓ 修复代码
// Use postMessage for cross-origin communication
iframe.contentWindow.postMessage("get-data", "https://other-origin.com");
window.addEventListener("message", (e) => {
  if (e.origin === "https://other-origin.com") console.log(e.data);
});

Direct DOM access 跨越原点是 blocked. Use postMessage for safe cross-原点 communication.

如何诊断

  • 检查如果目标是 on 不同领域, protocol, or port

  • 验证 CORS 请求头是 设置 on 服务器 for 获取请求

  • Look for iframe or window access 跨越原点

如何修复

  • 配置 CORS 请求头 on 服务器到 允许原点

  • Use postMessage for cross-原点 iframe communication

  • Proxy 请求通过你的 own 服务器到 avoid cross-原点 issues

如何预防

  • 设置 proper CORS 请求头 on APIs

  • Use postMessage 相反的 direct DOM access for cross-原点 iframes

  • Keep resources on 相同原点当 可能

相关资源

相关术语

相关课程

  • DOM

    学习关于 cross-原点 restrictions.

← 返回语言