Failed to execute 'postMessage' on 'DOMWindow'

頁(yè)面嵌入iframe,兩者之間通信的時(shí)候報(bào)錯(cuò)


image.png
  //http://localhost:5555/index.vue
 <iframe src="http://localhost:5556" frameborder="0" width="100%" height="100%"></iframe>
 //http://localhost:5556/index
 //5556頁(yè)面需要給5555頁(yè)面進(jìn)行傳遞信息
 window.parent.postMessage(
          {
            return: true,
          },
          // parentHost,
        );
這樣就會(huì)報(bào)錯(cuò)跨域問題,無(wú)法傳遞信息
//需要添加指定的origin,就能正常通信了
const parentHost = http://localhost:5555
window.parent.postMessage(
         {
           return: true,
         },
         parentHost,
       );
// locahost:5555接收信息
 window.addEventListener(
      'message',
      event => {
       console.log(event.data.return)
      },
      false,
    );

其他的推薦也有把window.parent.postMessage改成top.postMessage,但是我沒有添加origin的時(shí)候還是報(bào)錯(cuò),添加origin后兩者都支持

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容