天天看点

js 跨域访问

杞?甯??板??锛?http://blog.csdn.net/lanmao100/archive/2010/03/22/5404393.aspx

1 锛??ㄤ?绡???绔?涓????帮?涓? iframe 涓???锛?濡??? A 缃?绔?浠? B 缃?绔???杞?javascript 锛? A 缃?绔???浠ヨ???辩??璁块??璇?javascript ????瀹癸?骞朵?浼?琚?娴?瑙??ㄨ?や负??璺ㄥ??璁块????妯′豢???? iframe ????璺?锛?褰??ㄦ?风?瑰?绘??浜ゆ?讹???浠ュ?ㄦ????寤轰?涓? javascript 瀵硅薄锛?

var url? =? 'http://localhost:6604/WebSite2/Default.aspx?aa=bb';

var lastScript;

var h=document.getElementsByTagName("head")[0];

function loadScript(url){

var f=document.createElement("script");

var d=new Date().getTime();

f.type="text/javascript";

f.id=d;

f.setAttribute( "src" , url);

document.body.appendChild(f);

//澶?娆$?瑰?????や?娆$????杞界????瀹?if(lastScript&&g(lastScript))

??? g(lastScript).parentNode.removeChild(g(lastScript));

lastScript=d;

}

function g(x){return document.getElementById(x)};

Default.aspx 杈??虹??蹇?椤绘?? javascript 浠g??锛?瀹???浠ヨ??ㄧ?ㄦ?风?椤典????朵? javascript ?芥?帮?浠ュ????浣? dom 瀵硅薄??

???锛???瑕?娉ㄦ??????杩?绉??规?涓?瀹????ㄥ?颁? get ?规??ユ??浜や俊??锛?浠?涓?浜?璧???涓????帮? get ?规?姣?娆℃??浜ょ??淇℃??涓??借?杩? 2k

2锛?MSXML2.XMLHTTP 璺ㄥ??璁块??????绀烘??????璁块????瑙e?冲??娉?

protected void Page_Load(object sender, EventArgs e)

??? {

??????? if (Request["WEB"] != null)

??????? {

??????????? Response.Clear();

??????????? //Response.AppendHeader("Content-type", "text/xml;charset=GB2312");

??????????? System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)System.Net.HttpWebRequest.Create(Request["WEB"]).GetResponse();

??????????? System.IO.StreamReader sr = new System.IO.StreamReader(rs.GetResponseStream(), System.Text.Encoding.Default);

??????????? Response.Write(sr.ReadToEnd());

??????????? Response.End();

??????? }

??? }

Default.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

??? <title>??杩?JS?峰??寮?姝ユ?版??</title>

??? <script language="javascript" type="text/javascript">

??????? var xmlHttp=null;

??????? var xmlDom =null;

??????? var xslDom =null;

???????

??????? function send()

??????? {

??????????? xmlHttp=new ActiveXObject("MSXML2.XMLHTTP");

??????????? xmlDom =new ActiveXObject("MSXML2.DOMDocument");

???????????

??????????? //xmlHttp.onreadystatechange=doHttpReadyStateChange;

??????????? xmlHttp.open("POST","/XmlHttp/GetData.aspx?WEB=http://www.163.com",false);

??????????? xmlHttp.send("");

??????????? result=xmlHttp.responseText;

??????????? //alert(result);

//??????????? xmlDom.loadXML(result);

//??????????? items1=xmlDom.selectNodes("//NewDataSet/FriendLink/vc_LinkName");

//??????????? alert(items1.length);

//??????????? for(i=0;i<items1.length;i++)

//??????????? {

//??????????????? TextArea1.value+=items1(i).text;

//??????????? }

??????????? document.write(xmlHttp.responseText);

??????? }

???????

??????? function doHttpReadyStateChange()

??????? {

??????????? if(xmlHttp.readyState==4)

??????????? {

??????????????? TextArea1.value+=xmlHttp.responseText;

??????????? }

??????? }

??? </script>

</head>

<body>

??? <input id="Button1" type="button" value="??浜? 慰nclick="send();" />

??? <br />

??? <br />

??? <br />

??? <textarea id="TextArea1" style="width: 925px; height: 441px"></textarea>

</body>

</html>

?

?????ヨ??CSDN??瀹??杞?杞借?锋?????哄?锛?http://blog.csdn.net/lanmao100/archive/2010/03/22/5404393.aspx

============

???涓???甯?瀛?锛?http://topic.csdn.net/u/20090525/10/105319d9-2c11-4e95-b74d-e8282ee78a53.html

涓?zzxap????澶?1

1.IFrame?瑰?锛?杩?????浠?浠ュ??甯哥?ㄧ??涓?绉?浜や??瑰?锛?浣???娉?瀹??板??Google Map??51ditu???d?绠?娲????瑰?????浠ヨ?娣?姹般??

? 2.淇??规???″?ㄥ???版????寤虹?????″?ㄤ唬??锛???杩?瀹?浠?杩?琛?杞???????浠????″?ㄥ钩?版??Windows??锛?寰????炬??浠?涓??芥?ュ??杩?绉??瑰???

? 3.?╃?ㄤ釜??娴?瑙??ㄦ??渚???璺ㄥ???瑰?锛?????瑕?姹??ㄦ?锋???ㄨ?剧疆IE瀹??ㄦ??????瀹??ㄤ?绗?????浠?寮???浜哄???????э???涔??芥??宸ヤ?涓㈢??ㄦ?锋??渚?璧??朵?娴?瑙??ㄥ????

? 4.?ㄦ??Script????锛???浠ュ???拌法??璁块??锛?浣???????GET?瑰???杩???XML????Json?煎????版?????界?惰?XMLHttp姣?杈????瑰急寮辩????瑙?锛?浣?瀹?姣?绔?绗?????浠???瑕?姹????藉?杈惧?版??浠???????????涓??板?ㄦ???朵????戒娇?ㄨ?绉??瑰???

? 浠g??寰?绠???锛?

? 1.寤虹??涓?涓?Script瀵硅薄

? var so = document.createElement('SCRIPT');

? 2.璁剧疆杩?绋?璋??ㄧ?????涓??????板??璺?寰?缁?瑕?寤虹????Script瀵硅薄.(?㈠??浠ヤ??????颁???浠ユ????璋?Js?芥?板??绉?瑕?姹????″?ㄧ??浠g??寮???])

? so.src='http://www.xxx.com/xxx.aspx?xxxx';

? 3.?ㄦ??寤虹??Script瀵硅薄??锛?浣???浠ュ??ody??head涓???寤猴?

? document.body.appendChild(so);

? OK锛?

? 杩???涓や釜?充?璺ㄥ????棰?瑙e?虫?规???绮惧??甯?瀛?

? http://juliet.javaeye.com/blog/180011

??

? http://book.csdn.net/bookfiles/272/10027212025.shtml

?

zzxap????澶?2

?

JavaScript Ajax 璺ㄥ??

??杩?????涓?涓?椤圭??涓???瑕?ajax璺ㄥ????寰??版??,濡??????ㄦ????涓?纭?瀹?娌℃????棰?锛?浣????惧?颁?绾у?????朵???涓?娴?瑙??ㄧ?存?ュ氨寮瑰?烘??绀烘?:??璇ラ〉姝e??

1.浠?涔?寮?璧蜂?ajax璺ㄥ??涓??界????棰?

ajax??韬?瀹???涓?????杩?XMLHttpRequest瀵硅薄?ヨ?琛??版????浜や?锛???娴?瑙??ㄥ?轰?瀹??ㄨ????锛?涓???璁?s浠g??杩?琛?璺ㄥ????浣?锛???浠ヤ?璀?????

2.??浠?涔?瀹?缇???瑙e?虫?规?涔?锛?

娌℃????瑙e?虫?规???涓?灏?锛?浣??????芥???规????宸辩??瀹??????垫?ラ???┿??

?蜂????垫??:

涓?????????瀛??????镐?璁块??: www.aa.com??book.aa.com

浜??????????朵??????镐?璁块??: www.aa.com??www.bb.com ??iframe

涓??????????朵??????镐?璁块??: www.aa.com??www.bb.com ??XMLHttpRequest璁块??浠g??

???????????朵??????镐?璁块??: www.aa.com??www.bb.com ??JS??寤哄?ㄦ???????

瑙e?虫?规?锛?

涓???濡????冲???版?版????浜や?锛??d?www.aa.com??book.aa.com蹇?椤荤?变??ュ???????浠ャ????浠ュ?book.aa.com??frame娣诲????ww.aa.com????涓?椤甸???,??ww.aa.com??iframe???㈤?藉??涓?document.domain = "aa.com"锛?杩??峰氨??浠ョ?涓???浜?锛???浠ュ???拌法??璁块????灏卞??骞虫?跺??涓?涓???涓??跺?iframe涓??凤??存?ヨ??ㄩ???㈢??JS灏卞??浠ヤ???锛?杩?涓???娉???娌℃??灏?璇?锛?涓?杩???璁哄??琛?锛?

浜???褰?涓や釜??涓?????濡????崇?镐?璋?????d????烽??瑕?涓や釜???芥???变??ュ???????浠ャ????frame??浠ュ???版?版????浜??歌??ㄣ??瑙e?虫?规?灏辨????indow.location瀵硅薄??hash灞??с??hash灞??у氨??http://domian/web/a.htm#dshakjdhsjka???㈢??#dshakjdhsjka???╃??S?瑰??hash?肩?椤典?浼??锋?帮???浠ヨ??峰???伴??杩?JS璁块??hash?兼?ュ???伴??淇°??涓?杩??や?IE涔?澶??朵?澶ч?ㄥ??娴?瑙??ㄥ??瑕??瑰??hash灏变?璁板????诧?浣??ㄥ??杩????????跺氨??瑕?澶???锛???甯搁夯????涓?杩?????绠?????澶????惰?????浠ョ?ㄧ??锛??蜂???浠g??????涓??㈡??涓?杞姐??澶т???杩?绋???椤甸????椤甸???ㄤ?????涓?,b??杩?iframe娣诲??????锛?a??杩?JS淇???frame??hash?硷?b???㈠??涓?涓?????锛???涓?S???戒慨??ash锛??版???????瑰?????界????宸辨?ュ?ゆ??锛?锛?妫?娴?????hash?艰?淇??逛?锛?寰??颁慨?圭???硷?缁?杩?澶???杩???a??瑕????硷????ヤ慨????hash?硷?杩?涓??版?硅?娉ㄦ??锛?濡???a??韬????g??ヨ?㈤〉?㈢??璇?姣?濡?http://domian/web/a.aspx?id=3,??涓??存??arent.window.location????娉???寰??版????锛????锋?ユ病??????????璇?锛???瑕?a??杩?涓?浼?杩??ワ???浠ヤ?姣?杈?楹荤???锛??????????瑕???????锛?濡???hash??????璇?灏卞??寰?杩??????版??锛??????稿???澶?????

涓???杩?绉???褰㈡????缁?甯搁???扮??锛?涔????ㄧ????澶???浜???灏辨??www.aa.com??www.bb.com浣????戒慨?逛?涓?锛?涔?灏辨?????涓?涓?????浜虹??锛?浜哄?跺??璇?浣?浣?瑕???寰??版??灏辫?块??????杩??ュ???版??浠?涔??峰????锛?????杩????版????浠?涔??煎???????浣???瑕?????灏辨???ㄤ?????涓??板缓涓?涓?缃?椤碉?璁╂???″?ㄥ?诲??浜虹??缃?绔?涓???寰??版??锛???杩???缁?浣???domain1涓???a??????涓???GetData.aspx璇锋??版??锛?GetData.aspx??domain2涓???ResponseData.aspx????璇锋?,ResponseData.aspx杩????版??缁?GetData.aspx, GetData.aspx??杩???缁?a,杩??峰氨瀹???浜?涓?娆℃?版??璇锋???GetData.aspx?ㄥ?朵腑??褰?浜?浠g????浣??ㄣ???蜂???浠ョ??涓?????浠g????

????杩?涓???涓?涓????哄??灏辨??璇锋???浣跨??lt;script>??绛炬?ヨ?锋???锛?杩?涓?瑕?姹?涔???涓や釜???芥???变??ュ?????琛???????灏辨??JS??浠舵敞?ワ??ㄦ????????a??????涓?涓?JS??绛撅?瀹???SRC????璇锋??????涓?涓???????涓?椤甸??锛?b杩????版???冲??锛???浠ョ?存?ヨ???JS??浠g??????涓?cript??src灞??ф????浠ヨ法???????蜂???浠g??锛?杩?涓?涔?姣?杈?绠?????

code:

http://www.live-share.com/files/300697/Cross_The_Site_Test_code.rar.html

(csdn涓??界?璐撮??浠朵?锛?)

?荤?锛?

绗?涓?绉????碉?????瀛???????棰?锛???浠ュ???ㄨВ?充氦浜???

绗?浜?绉????碉?璺ㄥ??锛?瀹??拌?绋???甯搁夯?????瑕?涓や釜??寮??????借?芥?у?讹????ㄤ?绠???浜や???

绗?涓?绉????碉?璺ㄥ??锛?寮????????у?朵?涓????冲??锛?瀹??拌?绋???瑕?澧???浠g????寰??版??锛???甯哥?ㄧ???瑰???

绗???绉????碉?璺ㄥ??锛?涓や釜??寮??????介??瑕??у?讹?杩???涓?娈?s浠g????

PS:浠g????宸辨???ф???典慨?瑰?冲????

杩????垮??浜虹???????炬?ワ???缇?????绔?姣?杈?澶???

1. Security Considerations: Dynamic HTML

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/sec_dhtml.asp?

2. About Cross-Frame Scripting and Security

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/om/xframe_scripting_security.asp?

3. Cross-Domain Proxy

http://ajaxpatterns.org/Cross-Domain_Proxy?

4. Cross Domain XMLHttpRequest using an IFrame Proxy

http://manual.dojotoolkit.org/WikiHome/DojoDotBook/Book75?

5. Back Button Support for Atlas UpdatePanels

http://www.nikhilk.net/BackButtonSupport.aspx?

6. Cross-document messaging hack

http://blog.monstuff.com/archives/000304.html?

7. Building Mash-ups with "Atlas"

http://atlas.asp.net/docs/Walkthroughs/DevScenarios/bridge.aspx?

8. Calling web services hosted outside of your application with ??Atlas??

http://blogs.msdn.com/federaldev/archive/2006/07/31/684229.aspx?

http://www.federaldeveloper.com/Shared%20Documents/Presentations%20by%20Marc

20Schweigert/CallAtlasWebServiceInDifferentProject.zip?

9. AJAX Tip: Passing Messages Between iframes?

http://www.25hoursaday.com/weblog/PermaLink.aspx?guid=3b03cf9d-b589-4838-806e-64efcc0a1a15?

10. OSCON Cross-site Ajax Slides

http://blog.plaxo.com/archives/2006/07/oscon_crosssite.html?

http://www.plaxo.com/css/api/Joseph-Smarr-Plaxo-OSCON-2006.ppt?

11. OSCON 2006: Cross-site Ajax

http://www.sitepoint.com/blogs/2006/07/28/oscon-2006-cross-site-ajax/

zzxap????澶?3

1 锛??ㄤ?绡???绔?涓????帮?涓? iframe 涓???锛?濡??? A 缃?绔?浠? B 缃?绔???杞?javascript 锛? A 缃?绔???浠ヨ???辩??璁块??璇?javascript ????瀹癸?骞朵?浼?琚?娴?瑙??ㄨ?や负??璺ㄥ??璁块????妯′豢???? iframe ????璺?锛?褰??ㄦ?风?瑰?绘??浜ゆ?讹???浠ュ?ㄦ????寤轰?涓? javascript 瀵硅薄锛?

var url = 'http://localhost:6604/WebSite2/Default.aspx?aa=bb';

var lastScript;

var h=document.getElementsByTagName("head")[0];

function loadScript(url){

var f=document.createElement("script");

var d=new Date().getTime();

f.type="text/javascript";

f.id=d;

f.setAttribute( "src" , url);?

document.body.appendChild(f);?

//澶?娆$?瑰?????や?娆$????杞界????瀹?if(lastScript&&g(lastScript))

? g(lastScript).parentNode.removeChild(g(lastScript));

lastScript=d;

}

function g(x){return document.getElementById(x)};

Default.aspx 杈??虹??蹇?椤绘?? javascript 浠g??锛?瀹???浠ヨ??ㄧ?ㄦ?风?椤典????朵? javascript ?芥?帮?浠ュ????浣? dom 瀵硅薄??

???锛???瑕?娉ㄦ??????杩?绉??规?涓?瀹????ㄥ?颁? get ?规??ユ??浜や俊??锛?浠?涓?浜?璧???涓????帮? get ?规?姣?娆℃??浜ょ??淇℃??涓??借?杩? 2k?

2锛?MSXML2.XMLHTTP 璺ㄥ??璁块??????绀烘??????璁块????瑙e?冲??娉??

protected void Page_Load(object sender, EventArgs e)

? {

? if (Request["WEB"] != null)

? {

? Response.Clear();

? //Response.AppendHeader("Content-type", "text/xml;charset=GB2312");

? System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)System.Net.HttpWebRequest.Create(Request["WEB"]).GetResponse();

? System.IO.StreamReader sr = new System.IO.StreamReader(rs.GetResponseStream(), System.Text.Encoding.Default);

? Response.Write(sr.ReadToEnd());

? Response.End();

? }

? }

Default.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

? <title>??杩?JS?峰??寮?姝ユ?版??</title>

? <script language="javascript" type="text/javascript">

? var xmlHttp=null;

? var xmlDom =null;

? var xslDom =null;

? ?

? function send()

? {

? xmlHttp=new ActiveXObject("MSXML2.XMLHTTP");

? xmlDom =new ActiveXObject("MSXML2.DOMDocument");

? ?

? //xmlHttp.onreadystatechange=doHttpReadyStateChange;

? xmlHttp.open("POST","/XmlHttp/GetData.aspx?WEB=http://www.163.com",false);

? xmlHttp.send("");

? result=xmlHttp.responseText;

? //alert(result);

// xmlDom.loadXML(result);

// items1=xmlDom.selectNodes("//NewDataSet/FriendLink/vc_LinkName");

// alert(items1.length);

// for(i=0;i<items1.length;i++)

// {

// TextArea1.value+=items1(i).text;

// }

? document.write(xmlHttp.responseText);

? }

? ?

? function doHttpReadyStateChange()

? {

? if(xmlHttp.readyState==4)

? {

? TextArea1.value+=xmlHttp.responseText;

? }

? }

? </script>

</head>

<body>

? <input id="Button1" type="button" value="??浜? 慰nclick="send();" />

? <br />

? <br />

? <br />

? <textarea id="TextArea1" style="width: 925px; height: 441px"></textarea>

</body>

</html>

杞???锛?http://www.cnblogs.com/aspxcsharp/archive/2007/08/15/857003.html锛?

=======

?

?

继续阅读