使用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无法请求到 ...
随机推荐
- Android基础_web通信
一.发展史 1G 模拟制式手机,只能进行语音通话2G 数字制式手机,增加接收数据等功能3G 智能手机,它已经成了集语音通信和多媒体通信相结合,并且包括图像.音乐.网页浏览.电话会议以及其它一些信息服务 ...
- Centos7 Zookeeper
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.写在前面 ZK是一个高效的分布式协调服务,高可用的分布式管理协调框架. 朋友推荐一本书& ...
- echarts饼图点击事件
/** * 点击事件 */myChart2.on('click', function (param) { var index = param.dataIndex; alert(index);});
- LuceneNet 实现快速大文件大数据查询
做过站内搜索的朋友应该对Lucene.Net不陌生,因为用普通的sql like查询肯定是不行的,太慢了. 首先说明的是--Lucene.Net只是一个全文检索开发包,不是一个成型的搜索引擎, 它的 ...
- c#委托事件入门--第一讲:委托入门
说起委托,有些刚刚入门c#的人感觉很高大上,没有接触过,但是其实很多人都用过Lambda表达式,实际上Lambda表达式就是一个委托. 关于委托入门有个大神写的很详细:张子阳的博客 C#中的委托和事 ...
- md5加密以及可逆的加密解密算法
md5加密 package gov.mof.fasp2.gcfr.adjustoffset.adjust; import java.security.MessageDigest; public cla ...
- lnmp14最新版
系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian/Deepin Server/Aliyun/Amazon/Mint Linux发行版 需要5GB以上硬盘剩 ...
- php 5.0 与7.0有什么区别
我有更好的答案 发布于2017-05-19 12:30 最佳答案 PHP7特性 PHP 7.0.0 Alpha 1[1] 使用新版的ZendEngine引擎,带来了许多新的特性,以下是不完全列表: 性 ...
- 邓_ Php·魔术方法
================================================ 1.__tostring() 用于定义输出对象引用时调用 常用于打印一些对象的信息 必须有返回值 ...
- 数据库连接池(c3p0)
(一)问题的提出: 在使用开发基于数据库的web程序时,传统的数据库使用模式按照以下步骤: 在程序中建立数据库连接 进行sql操作 断开数据库连接 但是,这种模式存在着移动的问题: 传统连接模式每次向 ...