【问题标题】:same-origin request blocked as cross-origin同源请求被阻止为跨域
【发布时间】:2016-04-20 11:27:15
【问题描述】:

我在我管理的网站中有一些错误报告,并且每隔一段时间我就会收到 Uncaught SecurityError: Blocked a frame with origin "https://domain.com" from accessing a cross-origin frame 的错误报告。按照堆栈跟踪中的行号,我看到它是我访问 document.querySelector("iframe#results-map").contentWindow 的地方。然而,这个 iframe 是通过一个相对 URL 访问的:/html/googleresultsmap.html 所以它不能是跨域的(事实上它在 99.9% 的时间里都有效,因为我没有得到很多这样的报告)。所有报告都报告 Chrome(各种版本/变体)。

那么相对 URL 怎么会导致跨域框架呢?

<iframe src="/html/googleresultsmap.html" id="results-map"></iframe>

document.querySelector("iframe#results-map").contentWindow

【问题讨论】:

    标签: google-chrome cors


    【解决方案1】:

    我发现当document.domain 被明确设置在某个地方时就是这种情况。如果您尝试使用 Safari 开发人员工具,我发现它在这种情况下更有帮助,并显示如下消息:

    Blocked a frame with origin "{accessor}" from accessing a frame with origin "{accessee}". The frame requesting access set "document.domain" to "blah.com", but the frame being accessed did not. Both must set "document.domain" to the same value to allow access.
    

    【讨论】:

    • 我没有设置document.domain。它可能设置在哪里?也许是浏览器扩展?我将如何解决它?
    • 您是否尝试过使用 Safari 开发工具,它是否提供了更全面的错误消息?
    • 我无法复制它。它没有发生在我的机器上。它似乎只出现在 0.1% 的用户机器上。
    猜你喜欢
    • 2018-12-31
    • 2021-06-26
    • 2014-10-13
    • 2019-11-04
    • 2017-02-06
    • 2017-01-04
    • 2014-12-27
    相关资源
    最近更新 更多