一、h5 postMessage

node http-server配置服务器

有关配置:请参考我的http://www.cnblogs.com/leee/p/5502727.html

我把文件夹a配置成http://192.168.1.100:8000

文件夹b配置成http://192.168.1.100:7000

父页面获取iframe两种方式

1,window.frames["goalNameFrame"] 这个goalNameFrame是iframe的name值

2,goalFrame.contentWindow id值获取iframe,通过contentWindow 获得

子页面获取父页面两种

1,window.top

2,window.parent

本页面窗口两种,

1,window

2,self

请求,接受

请求:窗口.postMessage()方法参数(数据,域地址)

接受:message事件,e事件对象,e.data发过来的数据 e.origin过来请求的域名

a文件夹a1.htm

<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<script type="text/javascript">
window.onload=function(){
var goalFrame=document.getElementById("goalFrame");
var btn=document.getElementById("btn");
btn.onclick=function(){
//goalFrame.contentWindow通过iframe的id得到window
//window.frames["goalNameFrame"]通过iframe的name得到window //window.frames["goalNameFrame"].document.body.style.background = 'red';
//goalFrame.contentWindow.document.body.style.background = 'red'; //第一个参数:发送的数据
//第二个参数:接收数据的域名{带上协议}
//goalFrame.contentWindow.postMessage("1","http://192.168.1.100:7000/b1.htm");
window.frames["goalNameFrame"].postMessage("1","http://192.168.1.100:7000/b1.htm");
};
//iframe改变parent
self.addEventListener("message",function(e){
if (e.data=="2") {
document.body.style.background = 'green';
}
})
}
</script>
</head>
<body>
<iframe id="goalFrame" name="goalNameFrame" src="http://192.168.1.100:7000/b1.htm" ></iframe>
<button id="btn">变色</button>
</body>
</html>

b文件夹b1.htm

<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<script type="text/javascript">
window.onload=function(){
//window=self
//top //message事件的事件对象下保存了发送过来的内容
//ev.data : 发送过来的数据
//ev.origin
self.addEventListener('message',function(e){
if (e.data=="1") {
alert(e.origin) ;
document.body.style.background='red';
//父窗口变绿
window.top.postMessage('2',"http://192.168.1.100:8000/a1.htm")
}
})
} </script>
</head>
<body>
b1
</body>
</html>

二、ajax xmlhttprequest请求跨跨域

发送请求的页面,通过http-server部署到http://192.168.1.100:9005

<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<script type="text/javascript">
window.onload=function(){
var oBtn = document.getElementById('btn');
oBtn.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert(xhr.responseText);
}
}
}
xhr.open('get', 'http://192.168.1.100/WebForm1.aspx', true);
xhr.send();
}
}
</script>
</head>
<body>
<button id="btn">xhr2弹出跨域获得的内容</button>
</body>
</html>

要请求的页面,我通过vs将aspx发布到,本地iis http://192.168.1.100

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="server.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
server
</body>
</html>
namespace server
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://192.168.1.100:9005");
}
}
}

注意

google和firefox支持,但ie6 7 8 9不支持。支持ie6 7 8 9的是XDomainRequest

截图



三 、XDomainRequest 支持ie6 7 8 9的是XDomainRequest

https://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx

https://developer.mozilla.org/en-US/docs/Web/API/XDomainRequest

这里 67 89 支持 ie10就开始XMLHttpRequest了

var xhr = new XDomainRequst();
xhr.onload = function() {
alert(xhr.responseText);
}
xhr.open('get', 'http://192.168.1.100/WebForm1.aspx', true);
xhr.send();

javascript:cors跨域postMessage、xhr2和xmldomain的更多相关文章

  1. postmessage/cors跨域postMessage、xhr2和xmldomain

    一.h5 postMessage node http-server配置服务器 有关配置:请参考我的http://www.cnblogs.com/leee/p/5502727.html 我把文件夹a配置 ...

  2. jsonp与cors跨域的一些理解(转)

    CORS其实出现时间不短了,它在维基百科上的定义是:跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源.而这种访问是被同源策略所禁止的. ...

  3. Web APi之手动实现JSONP或安装配置Cors跨域(七)

    前言 照理来说本节也应该讲Web API原理,目前已经探讨完了比较底层的Web API消息处理管道以及Web Host寄宿管道,接下来应该要触及控制器.Action方法,以及过滤器.模型绑定等等,想想 ...

  4. Web API 实现JSONP或者安装配置Cors跨域

    前言 照理来说本节也应该讲Web API原理,目前已经探讨完了比较底层的Web API消息处理管道以及Web Host寄宿管道,接下来应该要触及控制器.Action方法,以及过滤器.模型绑定等等,想想 ...

  5. Web CORS 跨域方式使用方式

    CORS 参考 http://enable-cors.org/index.html https://help.aliyun.com/document_detail/oss/practice/cors_ ...

  6. CORS 跨域 实现思路及相关解决方案

    本篇包括以下内容: CORS 定义 CORS 对比 JSONP CORS,BROWSER支持情况 主要用途 Ajax请求跨域资源的异常 CORS 实现思路 安全说明 CORS 几种解决方案 自定义CO ...

  7. jsonp与cors跨域的一些理解

    浏览器的同源策略,即是浏览器之间要隔离不同域的内容,禁止互相操作. 比如,当你打开了多个网站,如果允许多个网站之间互相操作,那么其中一个木马网站就可以通过这种互相操作进行一系列的非法行为,获取你在各个 ...

  8. Api之Cors跨域以及其他跨域方式

    Web Api之Cors跨域以及其他跨域方式(三)   我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...

  9. CORS跨域资源共享

    CORS(跨域资源共享)跨域问题及解决 当使用ajax跨域请求时,浏览器报错:XmlHttpRequest error: Origin null is not allowed by Access-Co ...

随机推荐

  1. 交叉编译中的build、host和target

    build.host和target    在交叉编译中比较常见的一些参数就是build.host和target了,正确的理解这三者的含义对于交叉编译是非常重要的,下面就此进行解释 --build=编译 ...

  2. 使用Entity Framework通过code first方式创建数据库和数据表

    开发环境 WIN10 Entity Framework6.0  MVC5.0  开发工具 VS2015  SqlServer2012 1.创建上下文Context继承DbContext,并创建其他的业 ...

  3. iOS On-Demand Resources简单理解

    ios9引入了一个新功能,On-Demand Resources,它是app thinning 的一部分.这个机能简单的说,就是在下载app的时候,app中包含的不重要资源不下载,等到需要时,在由系统 ...

  4. SAP 销售订单的文本项目

    http://blog.itpub.net/9859323/viewspace-616508/ ls_hdname = wa_vbak-vbeln .     CALL FUNCTION 'READ_ ...

  5. [Android Pro] listView和GridView的item设置的高度和宽度不起作用

    referece to : http://blog.csdn.net/beibeixiao/article/details/9032569 1.     在Android开发中会发现,有时listVi ...

  6. CSS初始化样式

    为什么要初始化CSS? CSS初始化是指重设浏览器的样式.不同的浏览器默认的样式可能不尽相同,所以开发时的第一件事可能就是如何把它们统一.如果没对CSS初始化往往会出现浏览器之间的页面差异.每次新开发 ...

  7. php 总结第一篇(望大家补充!谢谢)

    /* 数组的常用函数 * * 数组的排序函数 *   sort() *   rsort() *   usort() *   asort() *   arsort() *   uasort() *   ...

  8. 终端下vim无法输入问题解决

    最近回归vim时发现会偶尔出现vim无法输入,但光标在动的情况,一度怀疑是spf13的问题,后来经搜索,才发现是因为vim下,快捷键 Ctrl+s 的功能是停止输入,在IDE下编程时间长了,都有潜意识 ...

  9. ASP.NET Core和Angular 2双剑合璧

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:两个还没有正式发布的东西一起用,是什么效果? 效果当然会很好了(我猜的),那么如何在A ...

  10. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...