HTML5解决跨域方案之postMessage
2018-01-31 11:19
381 查看
postMessage()方法可以使不同源的脚本采用异步方式进行数据传递
两个参数
data传递的数据,在传递参数的时候需要使用JSON.stringify()方法将对象参数序列化
origin
目标的源,协议,主机,端口号
将主页面和次级页面分别用node或者其他方法启动起来查看效果
详细说明在代码注释中
主页面
<body> <input type="button" value="点击发送数据到postmessage_01.html" onclick="sendInfoToAnotherDomain();"/> <iframe width="1200" src="http://localhost:8180"></iframe> <script> function sendInfoToAnotherDomain(){ var personInfo= new Object; // 要发送的数据 personInfo.name='Yuelu'; personInfo.title='Hello'; personInfo.info="World"; var str=JSON.stringify(personInfo); // 获取iframe var iframe=window.frames[0]; /** * [http description] * @type {[type]} 要发送的数据, 目标url */ // 传递数据 iframe.postMessage(str,'http://localhost:8180'); } </script> </body>
次级页面(iframe)
<body onload="receiveInfoFromAnotherDomain();"> <p>postmessage_01</p> </body> <script> function receiveInfoFromAnotherDomain(){ //监听窗口对象的message事件 window.addEventListener("message",function(ev){ //判断是否来自指定的url if(ev.origin !="http://localhost:8080"){ console.log("the event doesn't come from Domain1!"); return; } console.log(ev.data); //将json字符串转为json对象 var personInfoJSON = JSON.parse(ev.data); var name = personInfoJSON.name; var title = personInfoJSON.title; var info = personInfoJSON.info; //构造信息文本并显示 var personInfoString="从域为: "+ev.origin+"那里传来的数据."+"<br>"; personInfoString+="姓名是: "+name+"<br>"; personInfoString+="标题为: "+title+"<br>"; personInfoString+="信息为: "+info+"<br>"; document.body.innerHTML=personInfoString; } ); } </script>
相关文章推荐
- html5 postMessage解决跨域、跨窗口消息传递
- HTML5 postMessage解决跨域、跨iframe窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- [转]html5: postMessage解决跨域和跨页面通信的问题
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递[转载]
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递(转)
- html5中的postMessage解决跨域问题
- 使用HTML5中postMessage知识点解决Ajax中POST跨域问题
- html5 postMessage解决跨域、跨窗口消息传递
- html5的postmessage实现js前端跨域訪问及调用解决方式
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递
- html5 postMessage解决跨域、跨窗口消息传递