Uncaught SecurityError: Blocked a frame with origin ... from accessing a frame with origin 未捕获的安全错误:阻止了来自...源的框架访问具有源的框架。

4

我为一个 SAP 解决方案 (whatever) 制作了一个组件,通过 iframe 嵌入到报告中。在我将报告部署到 SAP 平台 (BO) 后,我在 Chrome 上遇到了这个错误 (但在 IE 或 FF 上也无法工作):

Uncaught SecurityError: Blocked a frame with origin "http://support.domain.com" from accessing a frame with origin "http://support.domain.com". The frame requesting access set "document.domain" to "domain.com", but the frame being accessed did not. Both must set "document.domain" to the same value to allow access.

该iframe嵌入到我的组件中,因此它应该在与报告相同的域和端口上运行。
我在SO中找到了这篇文章以及另一篇,但这并没有真正帮助我理解我需要做什么。
有没有一种方法可以摆脱这个问题,或者至少绕过这个问题? 谢谢:)
编辑: 主机页面URL:http://support.domain.com/BOE/OpenDocument/opendoc/openDocument.jsp?sIDType=CUID&iDocID=AbmffWLjCAlFsLj14TjuDWg 调用iframe上属性(并生成错误)的文件的URL:http://support.domain.com/BOE/OpenDocument/1411281523/zenwebclient/zen/mimes/sdk_include/com.domain.ds.extension/res/cmp/js/component.js 框架的URL:http://support.domain.com/BOE/OpenDocument/1411281523/zenwebclient/zen/mimes/sdk_include/com.domain.ds.extension/res/cmp/js/map/js/map.html 该iframe嵌入一些脚本标记,我可以在控制台的网络标记中看到所有加载正常的内容。
也许这能帮助解决问题。
第二次编辑: 我刚意识到SAP报告本身被嵌入到一个iframe中。这意味着我的iframe在一个iframe内部,这可能是问题所在。不过,当从Eclipse启动报告时,一切都正常。

@Marco Bonelli 我确实在发布我的帖子之前先查看了您的帖子,但问题并不相同,因为我的 iframe "技术上" 与我的顶部页面位于同一域中。也许我的标题不够明确。 - Stranded Kid
@SecularKid,我看了你的回答,可以向你保证这些是不同的域,因为我在链接的答案中指定了规则。如果你有subdimain.domain.com和domain.com,它们确实是不同的域,因为主机名是不同的,正如你所看到的那样。 - Marco Bonelli
@MarcoBonelli 好的,我明白了,我对“子域名”这个术语产生了误解,就像在这里解释的那样 https://razyr.zendesk.com/hc/en-us/articles/202651353--Subdomains-vs-Host-Names- 。我以为域名是像toto.com这样的,而在bob.toto.com中,bob是toto的子域名(从技术上讲确实是这样),因此属于与toto.com相同的域。 - Stranded Kid
然而,浏览器允许我在iframe侧将support.domain.com重写为domain.com,而没有任何警告。所以我有点迷失了。 - Stranded Kid
显示剩余5条评论
2个回答

7
我终于找到了解决方案。
我的iframe顶部的domain.location设置为domain.com,而我的iframe的domain.location设置为support.domain.com
尽管我仍然认为两者属于同一个域,但浏览器似乎不喜欢这种情况。
重新设置domain.location就可以解决问题。
对于那些问如何重新设置location.domain的人,这是我团队以前使用的代码片段。这很旧(2年前),并没有进行优化,我们已经不再使用它,但我想值得分享。 基本上,我们所做的是通过在URL参数中传递顶级域名来加载iframe
var topDomain = (function handleDomain(parameters) {
        if (typeof parameters === "undefined") {
            return;
        }
        parameters = parameters.split("&");
        var parameter  = [],
            domain;
        for (var i = 0; i<parameters.length; ++i) {
            parameter.push(parameters[i]);
        }
        for (var j = 0; j<parameter.length; ++j) {
            if (parameter[j].indexOf("domain") > -1) {
                domain = parameter[j];
                break;
            }
        }
        if (typeof domain !== "undefined") {
            domain = domain.split("=");
            return domain[1];
        }
        return; 
    })(window.location.search),
    domain = document.domain;

if (domain.indexOf(topDomain) > -1 && domain !== topDomain) {
    document.domain = topDomain;
}

你如何重置 domain.location - Trip

0

以前的答案已经不再有效:

Document.domain - https://developer.mozilla.org/zh-CN/docs/Web/API/Document/domain 已弃用:不再推荐使用此功能。尽管某些浏览器仍可能支持它,但它可能已从相关网页标准中删除、正在被删除的过程中,或仅用于兼容性目的。避免使用它,并在可能的情况下更新现有代码;请参阅本页面底部的兼容性表格以指导您的决策。请注意,此功能随时可能停止工作。

当前的解决方案是使用消息交换。请参见以下示例: 解决方案为https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接