2017-04-04 77 views
0

我有一个PHP页面中的iframe,我试图从iframe中获取父元素,但总是在控制台收到此错误:访问控制允许来源和document.domain的工作不

未捕获的抛出:DOMException:访问一个跨来源帧阻止与原籍 https://subdomain.domain.com的帧。

我已阅读谷歌的周围Same Origin policyAccess-Control-Allow-Origin第一页上的每一个搜索结果,但似乎并没有被解决我的问题。我仍然收到同样的错误。

这是我迄今所做的:

  1. 新增Access-Control-Allow-Origin到我的iframe中的内容。
  2. 增加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> 
+1

处理父窗口始终是一个糟糕的做法。即使域相同 –

+0

我理解你,但是当你在一个十来岁的应用与每天数百万用户的工作,你知道最轻微的修改是解决办法,而不是一个妥善的解决办法:)这是它是什么,我们必须找到现有系统的解决方案 –

+0

ok :)然后,您必须向父窗口(Access-Control-Allow-Origin)添加说明。然后,你可以通过postMessage的与它通信:https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage –

回答

0

无论到https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage你可以实现你的文件和iframe之间的通信。

在这种情况下,您必须指定将在父窗口上可用的所有操作。这里有一个例子:

父窗口:

<html> 
<head> 
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
</head> 
<body> 
<script> 
    function receiveMessage(event) { 
     console.log(event); 
     if (event.data.message === 'Hello world') { 
      $('#openFeedback').click(); 
     } 
    } 
    window.addEventListener("message", receiveMessage, false); 
</script> 


<div> 
    <button title="Close (Esc)" type="button" class="close">×</button> 
</div> 

<iframe name="myIframe" class="myIframe" 
     src="http://b.test.dev" width="100%" height="600px" 
     frameborder="0" allowfullscreen="" webkitallowfullscreen="" mozallowfullscreen=""> 
</iframe> 

</body> 
</html> 

和子在IFRAME:

<html> 
<head> 
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> 
</head> 
<body> 
<script> 
    window.parent.postMessage({message: 'Hello world'}, 'http://test.dev'); 
</script> 
</body> 
</html> 

你也应该为iframe的身份有些安全检查,就像你在你的例子添加。在这种情况下,你不需要来自IFRAME添加跨域头

+0

奇怪的是它为我工作,没有添加任何事件监听父母和添加iframe上的postMessage。我所做的只是在父母的头部添加了jquery文件,并且工作正常。 –

+0

对不起,我一直在同一个域上运行。让我试试看Prod –

+0

那么你成功了吗?:) –