【问题标题】:iframe same origin policy issueiframe 同源策略问题
【发布时间】:2016-07-05 13:14:27
【问题描述】:

在尝试读取 src 为其他域的 iframe 内容时出现以下错误

SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "https://qa-xxx.abc.com" from accessing a frame with origin "https://yyy.abc.com". Protocols, domains, and ports must match.

现在假设我现在在https://qa-xxx.abc.com doamin 并且我的 iframe 代码是

<iframe src="https://yyy.abc.com" style="height: 100%; width: 105%;" id="id_description_iframe"></iframe>

在搜索时我发现了这个解决方案Access-Control-Allow-Origin not working for iframe withing the same domain

所以我确实喜欢document.domain = 'abc.com'

同样的问题,任何解决方案都会有帮助。

【问题讨论】:

  • 如果您不控制 iframe 中的代码,您将无法在其中执行任何操作。你两个都控制吗?
  • @charlietfl 我没听明白,你说的 iframe 代码控制是什么意思
  • Simple... 是您控制的域上的 iframe,您可以将代码放入该页面吗?
  • 是的,我可以访问qa-xxx.abc.com,我只是在那里部署我的代码..
  • 这两者之间究竟需要做什么?我过去使用过 document.domain。但是postMessage API也是有用的api

标签: javascript jquery iframe


【解决方案1】:

您违反了同源政策。添加: document.domain = abc.com 到两个页面。可以直接在脚本标签中,也可以在引用的 js 文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-15
    • 2016-11-13
    • 2014-01-23
    • 1970-01-01
    • 2012-02-20
    • 2017-02-01
    • 2011-04-24
    • 2014-03-23
    相关资源
    最近更新 更多