同源请求被阻止为跨源

时间:2016-01-14 23:03:15

标签: google-chrome cors

我在我管理的网站上有一些错误报告,每隔一段时间我就收到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

1 个答案:

答案 0 :(得分:0)

我发现在某处显式设置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.