【发布时间】:2015-03-27 05:17:19
【问题描述】:
父页面位于 site.com。我的页面位于 sub.site.com 上,该页面将通过 iframe 包含在父页面中。根据https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy,这应该可以通过让 sub.site.com 页面将其域更新为 site.com 来实现。但是,当我尝试此操作时,由于相同的来源策略,我仍然会收到拒绝访问错误。
site.com:
<html>
<head></head>
<body><iframe src="http://sub.site.com"/></body>
</html>
sub.site.com:
<html>
<head>
<script>
function setupPage() {
try {
var oldDomain="current iframe domain: ";
oldDomain = oldDomain.concat(document.domain);
alert(oldDomain);
document.domain='sakai.rutgers.edu';
var newDomain="new iframe domain: ";
newDomain = newDomain.concat(document.domain);
alert(newDomain);
var parentDomain="parent domain: ";
parentDomain = parentDomain.concat(parent.window.document.domain);
alert(parentDomain);
}
catch (err) {
var e = "Caught error: ";
e = e.concat(err.message);
alert(e);
}
</script>
</head>
<body onload=setupPage()>
<p>Test Page</p>
</body>
当我加载父页面时,前两个警报显示域正在按预期重置。但是,当脚本尝试访问 parent.window.document.domain 时,会抛出以下错误(在 Chrome 中):
“捕获的错误:阻止具有源“http://sub.site.com”的框架访问跨域框架。”
我错过了什么?
【问题讨论】: