【发布时间】:2016-11-08 18:29:28
【问题描述】:
我有一个要求,我需要获取 iframe 的内容,其中 src 属于另一个域,下面是我的代码
<iframe id="receiver" src="http://demos.mattwest.io/post-message/receiver.html" width="500" height="200">
<p>Your browser does not support iframes.</p>
</iframe>
<script>
setTimeout(function(){
console.log($('#receiver').contents().find('body'));
}, 2000);
我收到以下错误
jquery-2.2.4.js:3079 Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "null" from accessing a frame with origin "http://demos.mattwest.io". The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http". Protocols must match.
我搜索了很多,发现关于PostMessage http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage
他们在这里发送消息,我的目标是获取 iframe 内容我如何在我的场景中实现 postMessage。
我无法创建小提琴,它显示了一些其他问题https://jsfiddle.net/alokranjan39/re6aja0c/
如何解决这个问题请帮忙!!!
【问题讨论】:
标签: javascript jquery iframe cross-domain