使用postMessage报错

来源:3-14 通信类

丶四夕

2019-07-02

老师你好,我用http-server启动了两个窗口。
A窗口(http://127.0.0.1:8000/a)代码如下:

 <div class="A">A</div>
  <iframe src="http://127.0.0.1:3000/b" frameborder="0"></iframe>
  <script>
    var BWindow = document.querySelector('iframe').contentWindow
    BWindow.postMessage('helloB',' http://127.0.0.1:3000/b')
    window.addEventListener('message', function(){
      console.log(111)
    })
 <div class="B">B</div>
    <div class="e"></div>
    <script>
      window.addEventListener('message', function(e) {
        e.source.postMessage('hello a ', e.origin)
      })
    </script>

最后执行报错,请问是为什么呢
Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘http://127.0.0.1:3000’) does not match the recipient window’s origin (‘http://127.0.0.1:8000’).

写回答

1回答

快乐动起来呀

2019-07-30

课程已经更新了源代码,看下https://git.imooc.com/coding-129/coding-129 postMessage这个目录

0
0

前端跳槽面试必备技巧 面试官全流程指导

一门让你能成功获取前端 Offer 的课程, 不只是技术,面试“技巧”也至关重要

4836 学习 · 830 问题

查看课程