原理核心:window对象的name属性是一个很特别的属性,当该window的location变化,然后重新加载,它的name属性可以依然保持不变。
依此原理,我们可以在页面A中用iframe加载其他域的页面B,而页面B中用JavaScript把需要传递的数据赋值给 window.name,页面A的iframe加载完成之后,页面A修改iframe的地址,将其变成同域的一个地址,然后就可以读出window.name的值了。
 
例:有两个网站www.a.com和www.b.com,我们要在www.a.com/a.html下获取www.b.com/data.html数据。
 
我们需要三个文件:
www.a.com 下的 a.html 获取数据并显示
www.b.com 下的data.html 提供数据
www.a.com 下的proxy.html 代理文件,与a.html同一域下,一般为空html文件。
 
www.b.com下的data.html如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
//添加需要传递的数据,大小一般为2M,IE和firefox下可以大至32M左右
window.name = '[{"name":"test1"},{"name":"test2"}]';
</script>
</body>
</html>
www.a.com下的proxy.html如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 空的html文件 -->
</body>
</html>
www.a.com下的a.html如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <!-- 用于引用www.b.com/data.html文件 -->
<iframe id="iframe" src=""></iframe> <!-- 显示获取到的数据 -->
<div id="data"></div> <script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
var ifr = document.getElementById("iframe");
ifr.src = "http://www.b.com/data.html";
if (ifr.attachEvent) {
ifr.attachEvent("onload", loadfunc);
} else {
ifr.onload = loadfunc;
} var state = 0;
function loadfunc() {
if(state == 0) {
state = 1;
ifr.contentWindow.location = "http://www.a.com/proxy.html";
} else {
var data = ifr.contentWindow.name;
$.each($.parseJSON(data), function(i, v) {
$("#data").append(v.name);
}); //销毁iframe,保证安全
ifr.contentWindow.document.write("");
ifr.contentWindow.close();
document.body.removeChild(ifr);
}
}
</script>
</body>
</html>

PHP下ajax跨域的解决方案之window.name的更多相关文章

  1. PHP下ajax跨域的解决方案之jsonp

    首先要说明一下json和jsonp的区别? json是一种基于文本的数据交换方式,或者叫做描述数据的一种格式. var person = { "name": "test& ...

  2. PHP下ajax跨域的解决方案之CORS

    由于安全的限制(同源策略,javascript只能访问同域名下的内容),如果需要进行跨域操作,那就免不了要进行跨域.   CORS(跨域资源共享,Cross-Origin Resource Shari ...

  3. ie8或9下ajax跨域问题

    ie8或9下ajax跨域支持,添加如下代码 <!--[if (IE 8)|(IE 9)]><script src="https://cdn.bootcss.com/jque ...

  4. java、ajax 跨域请求解决方案('Access-Control-Allow-Origin' header is present on the requested resource. Origin '请求源' is therefore not allowed access.)

      1.情景展示 ajax调取java服务器请求报错 报错信息如下: 'Access-Control-Allow-Origin' header is present on the requested ...

  5. 【JS】AJAX跨域-JSONP解决方案(一)

    AJAX跨域介绍 AJAX 跨域访问是用户访问A网站时所产生的对B网站的跨域访问请求均提交到A网站的指定页面 由于安全方面的原因, 客户端js使用xmlhttprequest只能用来向来源网站发送请求 ...

  6. ajax跨域问题解决方案

    今天来记录一下关于ajax跨域的一些问题.以备不时之需. 跨域 同源策略限制 同源策略阻止从一个域上加载的脚本获取或操作另一个域上的文档属性.也就是说,受到请求的 URL 的域必须与当前 Web 页面 ...

  7. 七牛---以一个七牛上传的实例小结下AJAX跨域【转】

    http://blog.csdn.net/netdxy/article/details/50699842 使用七牛过程中,很多用户或多或少遇到跨域的问题,这篇文章主要介绍下跨域的概念来看什么情况下会出 ...

  8. ajax跨域请求解决方案

    大家好,今天我们学习了js的跨域请求的解决方案,由于JS中存在同源策略,当请求不同协议名,不同端口号.不同主机名下面的文件时,将会违背同源策略,无法请求成功!需要进行跨域处理! 方案一.后台PHP进行 ...

  9. Ajax跨域访问解决方案

    No 'Access-Control-Allow-Origin' header is present on the requested resource. 当使用ajax访问远程服务器时,请求失败,浏 ...

随机推荐

  1. SVN - 简单使用手册

    背景 由于项目需要,新增了两名程序员来一起支持一个分支的开发工作,因此需要在原来的SVN中制作分支并且为new commer  分配用户以及权限. 0. 准备 在Window系统上使用SVN,我们最好 ...

  2. Spark分析之DAGScheduler

    DAGScheduler概述:是一个面向Stage层面的调度器: 主要入参有: dagScheduler.runJob(rdd, cleanedFunc, partitions, callSite, ...

  3. 给iOS开发新手送点福利,简述UIImagePickerController的属性和用法

    1.+(BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;         // 检查指定源是否在设备上 ...

  4. javascript日期相减,求时间差

    //计算时间差 var from_date = new Date(from_time); var end_date = new Date(end_time); var time_different = ...

  5. 什么是 web 开发

    什么是 web 开发     这几天因为工作需要,了解了一下Web development 的技术路线,来源自     en.wikipedia.org/wiki/Web_development    ...

  6. 2k8 32bit下载

    Windows Server 2008 32-bit Standard(标准版) Windows Server 2008 32-bit Enterprise(企业版) Windows Server 2 ...

  7. WPF 自定义属性

    做了一个自定义控件和一个自定义Grid,里面的元素可以随着绑定属性变化: 效果图(一定滑块): 关键代码: 1.自定义属性代码: public class MyGrid : Grid { public ...

  8. leetcode973

    public class POINT { public int X; public int Y; public int Z; } public class Solution { public int[ ...

  9. 视频地址blog加密

    /* JS部分 没处理兼容什么的 */ var id='<?php echo $_GET['id'];?>'; var video = document.getElementById(&q ...

  10. ADO 读写文本文件

    ' 创建配置文件            Open ThisWorkbook.Path & "\schema.ini" For Append As #1            ...