我有一个PHP页面中的iframe,我试图从iframe中获取父元素,但总是在控制台收到此错误:访问控制允许来源和document.domain的工作不
未捕获的抛出:DOMException:访问一个跨来源帧阻止与原籍 https://subdomain.domain.com的帧。
我已阅读谷歌的周围Same Origin policy
和Access-Control-Allow-Origin
第一页上的每一个搜索结果,但似乎并没有被解决我的问题。我仍然收到同样的错误。
这是我迄今所做的:
- 新增
Access-Control-Allow-Origin
到我的iframe中的内容。 - 增加
document.domain
到我的iframe中的内容。
有其他人有类似的问题?我能做些什么来解决它?
这里是我的代码:
<?php
header('Access-Control-Allow-Origin: https://b.example.com');
?>
<script>
function receiveMessage(event) {
if (event.origin !== "https://a.example.com")
return;
}
window.addEventListener("message", receiveMessage, false);
</script>
<div>
<button title="Close (Esc)" type="button" class="close">×</button>
</div>
<iframe name="myIframe" class="myIframe"
src="https://b.example.com" width="100%" height="600px"
frameborder="0" allowfullscreen="" webkitallowfullscreen="" mozallowfullscreen="">
</iframe>
</div>
脚本在b.example.com
<script>
window.parent.$('.close').click(function() {
var thisObj = this;
if (!feedbackLoaded) {
$('#openFeedback').click();
feedbackLoaded = true;
$('#feedbackForm .close').click(function() {
window.parent.$.magnificPopup.proto.close.call(thisObj);
});
return false;
} else {
window.parent.$.magnificPopup.proto.close.call(thisObj);
}
});
</script>
处理父窗口始终是一个糟糕的做法。即使域相同 –
我理解你,但是当你在一个十来岁的应用与每天数百万用户的工作,你知道最轻微的修改是解决办法,而不是一个妥善的解决办法:)这是它是什么,我们必须找到现有系统的解决方案 –
ok :)然后,您必须向父窗口(Access-Control-Allow-Origin)添加说明。然后,你可以通过postMessage的与它通信:https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage –