使用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无法请求到 ...
随机推荐
- flask-script组件
Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...
- JDBC简单范例
连接工具类 import java.sql.Connection; import java.sql.DriverManager; public class DBUtil { // 建立连接方法 pub ...
- PEP8 Python 编码规范整理
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...
- HDU2063-过山车-匈牙利算法
过山车 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- POJ2407-Relatives-欧拉函数
欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 对于一个正整数N的素数幂分解N=P1^q1*P2^q2*...*Pn^qn. Euler函数表达通式:euler(x)=x(1 ...
- hdu3076—概率dp
hdu3076-概率dp 标签 : 概率dp 题目链接 题意: 2个人分别有AB的血数,轮流扔骰子,数小的自减一血,平的不变,谁先到减0, 谁输,问A赢的概率. 题解: dp[i][j]表示的是第一个 ...
- sscanf()用法
http://blog.chinaunix.net/uid-26284412-id-3189214.html #include<cstdio> #include<cstring> ...
- 数值积分之Simpson公式与梯形公式
Simpson(辛普森)公式和梯形公式是求数值积分中很重要的两个公式,可以帮助我们使用计算机求解数值积分,而在使用过程中也有多种方式,比如复合公式和变步长公式.这里分别给出其简单实现(C++版): 1 ...
- 把自己的js模块兼容到AMD CMD CommonJS
为了让同一个模块可以运行在前后端,在写作过程中需要考虑兼容前端也实现了模块规范的环境.为了保持前后端的一致性,类库开发者需要将类库代码包装在一个闭包内.以下代码演示如何将hello()方法定义到不同的 ...
- UserView--第二种方式(避免第一种方式Set饱和),基于Spark算子的java代码实现
UserView--第二种方式(避免第一种方式Set饱和),基于Spark算子的java代码实现 测试数据 java代码 package com.hzf.spark.study; import ...