頁(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后兩者都支持