1.jsonp
requestpage:
function jsonpCallback(jsonp)
{
alert(jsonp.name);
}
function test()
{
var ele = document.createElement("script");
ele.src = "WebForm2.aspx?callback=jsonpCallback";
document.getElementsByTagName("head")[0].appendChild(ele);
}
responsepage:
protected void Page_Load(object sender, EventArgs e)
{
string callback = Request.QueryString["callback"];
Response.Write(callback+"({'name':'lilei','sex':'男'})");
}
2.window.name
requestpage:
<iframe id="if1" ></iframe>
//window.name
var state = 0;
function test2() {
var if1 = document.getElementById("if1");
window.name = "1";
if1.src = "WebForm6.aspx";
if1.onload = IframLoadFn;
}
var IframLoadFn = function () {
var if1 = document.getElementById("if1");
if (state == 1) {
var data = window.name; // 读取数据 if1.contentWindow.name
alert(data);
} else if (state == 0) {
state = 1;
if1.src = "Proxy.htm"; // 设置的代理文件和请求页面同一个域,此文件不一定要真实存在
}
};
responsepage:
<script type="text/javascript">
window.name = "{'name':'lilei','sex':'男'}";
</script>
3.iframe
requestpage:
<iframe id="if1" ></iframe>
function test1()
{
var if1=document.getElementById("if1");
if1.src = "WebForm4.aspx?callback=jsonpCallback";
}
responsepage:
<iframe id="pro" ></iframe>
<script type="text/javascript">
function _getQuery(key) {//通用方法,获取url参数
var query = location.href.split("?")[1];
var value = decodeURIComponent(query.split(key + "=")[1].split("&")[0]);
return value;
}
var callback = _getQuery("callback");
var pro = document.getElementById("pro");
pro.src = "proxy.aspx?callback=" + callback + "&data=" + "{'name':'lilei','sex':'男'}";
</script>
proxypage:和requestpage同域
<script type="text/javascript">
function _getUrl(key) {//通用方法,获取URL参数
var query = location.href.split("?")[1];
var value = decodeURIComponent(query.split(key + "=")[1].split("&")[0]);
return value;
}
(function() {
var callback = _getUrl("callback");
var data = _getUrl("data");
window.top[decodeURIComponent(callback)](eval('('+decodeURIComponent(data)+')'));
})() ;
</script>