javascript:cors跨域postMessage、xhr2和xmldomain
一、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的更多相关文章
- postmessage/cors跨域postMessage、xhr2和xmldomain
一.h5 postMessage node http-server配置服务器 有关配置:请参考我的http://www.cnblogs.com/leee/p/5502727.html 我把文件夹a配置 ...
- jsonp与cors跨域的一些理解(转)
CORS其实出现时间不短了,它在维基百科上的定义是:跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源.而这种访问是被同源策略所禁止的. ...
- Web APi之手动实现JSONP或安装配置Cors跨域(七)
前言 照理来说本节也应该讲Web API原理,目前已经探讨完了比较底层的Web API消息处理管道以及Web Host寄宿管道,接下来应该要触及控制器.Action方法,以及过滤器.模型绑定等等,想想 ...
- Web API 实现JSONP或者安装配置Cors跨域
前言 照理来说本节也应该讲Web API原理,目前已经探讨完了比较底层的Web API消息处理管道以及Web Host寄宿管道,接下来应该要触及控制器.Action方法,以及过滤器.模型绑定等等,想想 ...
- Web CORS 跨域方式使用方式
CORS 参考 http://enable-cors.org/index.html https://help.aliyun.com/document_detail/oss/practice/cors_ ...
- CORS 跨域 实现思路及相关解决方案
本篇包括以下内容: CORS 定义 CORS 对比 JSONP CORS,BROWSER支持情况 主要用途 Ajax请求跨域资源的异常 CORS 实现思路 安全说明 CORS 几种解决方案 自定义CO ...
- jsonp与cors跨域的一些理解
浏览器的同源策略,即是浏览器之间要隔离不同域的内容,禁止互相操作. 比如,当你打开了多个网站,如果允许多个网站之间互相操作,那么其中一个木马网站就可以通过这种互相操作进行一系列的非法行为,获取你在各个 ...
- Api之Cors跨域以及其他跨域方式
Web Api之Cors跨域以及其他跨域方式(三) 我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...
- CORS跨域资源共享
CORS(跨域资源共享)跨域问题及解决 当使用ajax跨域请求时,浏览器报错:XmlHttpRequest error: Origin null is not allowed by Access-Co ...
随机推荐
- 【Alpha版本】 第一天 11.7
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 前段时间完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 部分APP功能 我要招聘详情的展示 注册界面的实现 一些特殊效 ...
- MySQL 5.5编译安装
MYSQL数据库安装方法 yum/rpm方式安装mysql 只要执行yum install mysql-server即可. yum/rpm方式安装mysql应用场景:yum/rpm安装适用对数据库要求 ...
- Google疯了,竟然这样!
导读 一个小问题:你每天做什么事?当然了,好多事情,但是我可以指出一件事,你几乎每天都会用 Google 搜索,我说的对吗?现在,如果你是一位 Linux 用户,这里有另外一个问题:如果你甚至不用离开 ...
- git 教程(15)--分支管理策略
通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的comm ...
- Python入门(一)
Python版本:Python 2.7.5 Python是一种面向对象.解释型计算机程序设计语言 1.基本操作符python的除法的结果会随着数值类型的变化而变化整数相除,结果会取整实数相除,结果会取 ...
- 编译Android源码
编译版本要求 基本安装环境 ubuntu 14.04 64 sudo apt-get install git-core gnupg flex bison gperf build-essential \ ...
- Python操作rabbitmq 实践笔记
发布/订阅 系统 1.基本用法 生产者 import pika import sys username = 'wt' #指定远程rabbitmq的用户名密码 pwd = ' user_pwd = p ...
- java 深入技术三(List)
List ArrayList List接口 List接口的父接口-Collection List接口的重要子类- ArrayList -LikedList List接口不重要子类-Vector jav ...
- poj2492_A Bug's Life_并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34947 Accepted: 11459 D ...
- WdatePicker.js开始日期和结束日期比较
jQuery.validator.addMethod("endDate", function(value, element) { var start ...