iframe跨域问题
# iframe跨域问题
成功解决Blocked a frame with origin "http://127.0.0.1:8080" from accessing a cross-origin frame.
解决方法:
假如当前的项目的域名端口是 http://192.168.0.1:8080,通过页面的 iframe访问 http://127.0.0.1:8080,并接收 http://127.0.0.1:8080/file_system/传过来的消息。
<iframe width="100%" height="100%" frameborder="0" src="http://127.0.0.1:8080/file_system/"></iframe>
1
如果这是直接在子页面上调用 parent.document.getElementById("parentUrl").value=''给父页面传值或者使用 top.location.pathname,就遇到上面跨域问题。
解决方法就是:
在父页面添加监听器,接收子页面的值:
window.addEventListener('message',function(e){
alert(e.data);
},false);
1
2
3
2
3
在子页面在要传值的地方调用以下语句传值:
window.parent.postMessage('值','http://192.168.0.1:8080');
1
如果是非必需的功能,可以用 try {}catch(){}去捕捉问题,避免页面崩溃
编辑 (opens new window)
上次更新: 2025/04/18, 01:42:12