使用postMessage实现跨域 解决'Failed to execute 'postMessage' on 'DOMWindow''
使用iframe+postMessage解决跨域问题,首先来过一遍其中的原理咯
原理:
发送方使用postMessage方法向接收方推送消息,第一个参数为推送的内容,第二个参数是允许被访问的域名;
接收方通过监听message的方法接收数据。
实现跨域就需要有两个不同源的服务器咯
我在本地开启了两个不同端口的tomcat;(以下是我的文件路劲)
①tomcat/webapps/iframe/parent.html(端口号8088)
②tomcat1/webapps/iframe/child.html(端口号8089)
接下来开始编码
tomcat/webapps/iframe/parent.html:
<iframe src="localhost:8089/iframe/iframe.html" frameborder="1" id="ifr1" name="ifr1" scrolling="yes">
<p>Your Browser dose not support iframes</p>
</iframe>
<input type="text" id="message">
<input type="button" value="this is message" onclick="sendIt()">
<script>
var myIframe = document.getElementById('ifr1')
function sendIt () {
myIframe.contentWindow.postMessage(document.getElementById('message').value, 'localhost:8089')
} </script>
tomcat1/webapps/iframe/child.html:
window.addEventListener('message', function (e) {
alert(e.data)
})
理想状态-YY中:
parent页面通过iframe插入child页面,在输入框中输入内容,然后通过postMessage方法将内容作为信息推送给child,child页面通过监听message方法来接收数据,完美啊!
刷新运行
啪!打脸!!!

这什么鬼?
“提供的来源('localhost://')”与接收方('http://localhost:8088')的来源不匹配
不懂啊,这怎么搞,找一找茬,难道是少了http开头的协议?
试一下:
tomcat/webapps/iframe/parent.html:
<iframe src="http://localhost:8089/iframe/iframe.html" frameborder="1" id="ifr1" name="ifr1" scrolling="yes">
<p>Your Browser dose not support iframes</p>
</iframe>
<input type="text" id="message">
<input type="button" value="this is message" onclick="sendIt()">
<script>
var myIframe = document.getElementById('ifr1')
function sendIt () {
myIframe.contentWindow.postMessage(document.getElementById('message').value, 'http://localhost:8089')
}
window.addEventListener('message', function (e) {
alert(e.data)
})
</script>
刷新运行
阔以了!(是的可以了,就这么简单)
接下来实现在parent中获取到child中传来的信息:
tomcat/webapps/iframe/parent.html:
<iframe src="http://localhost:8089/iframe/iframe.html" frameborder="1" id="ifr1" name="ifr1" scrolling="yes">
<p>Your Browser dose not support iframes</p>
</iframe>
<input type="text" id="message">
<input type="button" value="this is message" onclick="sendIt()">
<script>
var myIframe = document.getElementById('ifr1')
function sendIt () {
myIframe.contentWindow.postMessage(document.getElementById('message').value, 'http://localhost:8089')
}
window.addEventListener('message', function (e) {
alert(e.data)
})
</script>
增加了对message的监听事件
tomcat1/webapps/iframe/child.html:
<input type="button" name="demoBtn" id="demoBtn" value="click">
<script>
window.addEventListener('message', function (e) {
console.log(e)
if (e.data.type === 'article') {
alert(e.data.msg.success)
} else {
alert(e.data)
}
})
function showTop () {
console.log('你好!')
}
document.getElementById('demoBtn').onclick = function () {
top.postMessage('hedfs', 'http://localhost:8088')
}
</script>
向'http://localhost:8088'域下的文件传参'hedfs'
刷新运行

OK!完成了,以上便是postMessage配合iframe跨域的方案思想
使用postMessage实现跨域 解决'Failed to execute 'postMessage' on 'DOMWindow''的更多相关文章
- JavaScript跨域解决办法
在找到跨域解决办法之前,我们要先弄清楚一些基本概念 什么是跨域? 什么是“同源策略”? 跨文档消息通信 & 跨域请求数据 主域相同而子域不同 不同域名的跨域访问 什么是跨域? 简单地理解就是因 ...
- js跨域解决方式
什么是跨域? 概念:仅仅要协议.域名.port有不论什么一个不同,都被当作是不同的域.(所谓同源是指,域名.协议,port同样.),对于port和协议的不同,仅仅能通过后台来解决. URL 说明 是否 ...
- 在javascript中的跨域解决
跨域产生的原因 跨域是由浏览器的同源策略引起的,即不同源(协议,域名,端口中其中有一个不同)的js是不能读取对方的资源的.当要网站中的js要请求其他网站的数据时就会产生跨域问题,就像下面这样,浏览器会 ...
- JavaScript跨域解决方法大全
跨域的定义:JavaScript出于安全性考虑,同源策略机制对跨域访问做了限制.域仅仅是通过“URL的首部”字符串进行识别,“URL的首部”指window.location.protocol +win ...
- JAVA联调接口跨域解决办法
JAVA联调接口跨域解决办法 第一种代码: HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus. ...
- [转]vue跨域解决方法
vue跨域解决方法 vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代码的时候就会报“No 'Access-Control-Allow-Origin' hea ...
- Html5 postMessage实现跨域消息传递
一.同源策略 要理解跨域,我们首先要知道什么是同源策略.百度百科上这样定义同源策略:同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略, ...
- spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置
spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...
- ssm跨域解决
最近挑战杯项目要交了,最后一个开发的项目,还是得好好对待,不知道会不会真香,昨天还是遇到了一些问题,尤其是对接的时候,用postman对接的时候,没有啥问题,结果前端上线对接时,发现ajax无法请求到 ...
随机推荐
- 针对Oracle的审计方案
主题:针对Oracle的审计方案 数据库环境:Oracle 11g 数据库审计需求: 1.需要对连接数据库的行为进行审计 2.需要对核心表的DML操作进行审计 3.需要迁移审计数据到指定表空间 4.需 ...
- ssm maven spring AOP读写分离
ssm maven spring AOP读写分离 总体流程 配置最开始写在pom.xml文件,解析到数据库配置文件,再解析到spring配置文件. 自定义注解DataSource:通过这个注解并且在s ...
- 电视盒子好用又强大的跨屏远程输入法 TVRemoteIME
应用包下载地址: https://github.com/kingthy/TVRemoteIME/raw/master/released/IMEService-release.apk TVRemoteI ...
- Android应用程序使用两个LinearLayout编排5个Button控件
学习存档: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...
- bzoj:1681 [Usaco2005 Mar]Checking an Alibi 不在场的证明
Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's c ...
- angular2 Http和websocket
1. 注入HttpModule模块: 2. 注入http服务 map方法需要导入"rajx/Rx"组件,作用是针对流的处理.Json是将流转化为json格式.subscribe订阅 ...
- vue安装babel依赖报错
刚刚因为一些原因把依赖包删除,重新install了一下,结果报错 deprecate babel-preset-es2015@* ???? Thanks for using Babel: we rec ...
- [国嵌攻略][066][ARP协议实现]
以太网通讯 在计算机网络中,数据发送的过程就是把数据按照各层协议层层封装的过程.在这个过程中,最终要使用的协议通常是以太网协议(数据链路层协议). 以太网包格式 目的MAC地址:接收者的物理地址(6字 ...
- QQ邮箱开启SMTP服务的步骤
首先要确保你的QQ邮箱已经要开启超过一个月.对于新开启的邮箱,腾讯是不开放这些功能的. 方法/步骤 首先点QQ头像旁边的信封符号进入邮箱. 当然你也可以使用 mail.qq.com进邮箱 进入邮箱后点 ...
- 微信小程序学习笔记
一.文件结构解析 pages文件夹: 书写各个页面代码以及组件.内部js文件书写js ; wxml文件为HTML ; wxss文件为css样式 : json文件为配置当前页面的默认项,如titl ...