Failed to execute ‘postMessage‘ on ‘DOMWindow‘: The target origin provided (‘http://localhost:50

报错: Failed to execute ‘postMessage’ on ‘DOMWindow’: The target origin provided (‘http://localhost:5001’) does not match the recipient window’s origin (‘http://localhost:5000’).

problem

本地2个服务 localhost:5000 localhost:5001,其中5001页面作为iframe嵌入到5000页面中
5000服务可以展示5001的页面,但是2个服务无法通过postmessage进行通信,控制台报错:
报错: Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:5001') does not match the recipient window's origin ('http://localhost:5000').

reason

跨域导致

solution

通过nginx转发,使其2个服务同源,然后通过路由前缀区分
同源之后,2个服务postmessage可以正常通信

附上nginx配置

server {
    listen       9999;
    server_name  localhost;
    # http://localhost:9999/parent/
    location /parent {
        proxy_pass http://localhost:5000/;
    }
    # http://localhost:9999/child/
    location /child {
        proxy_pass http://localhost:5001/;
    }
}

效果
Snipaste_2024-05-05_12-33-39

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值