Javascript跨域访问解决方案

来源:互联网 发布:adobe flash mac 中文 编辑:程序博客网 时间:2024/05/16 05:14

原文地址:http://blog.csdn.net/lovingprince/article/details/2954675

由于安全方面的考虑, Javascript 被限制了跨域访问的能力,但是有时候我们希望能够做一些合理的跨域访问的事情,那么怎么办呢?

 

这里分两类情况:


         一、基于同一父域的子域之间页面的访问 
                 参见如下 3 个 domain 域:

1 、 taobao.com
          2 、 jipiao.taobao.com
          3 、 promotion.taobao.com

它们有相同的父域   taobao.com


         二、基于不同父域页面之间的访问 
                 参见如下 3 个 domain 域:

1 、 taobao.com
             2 、 baidu.com

3 、 sina.com.cn

     它们具有不同的父域。

 

解决它们之间跨域的方案:

<!--[if !supportLists]-->①<!--[endif]-->服务器 Proxy:   域 A 的页面 JS 需要访问域 B 下的链接获取数据,该方案在域 A 的服务器端建立一个Proxy 程序 ( 可能是 ASP 、 servlet 等任何服务端程序 ) ,域 A 的页面 JS 直接调用本域下的 Proxy 程序, proxy 程序负责将请求发送给域 B下的链接并获取到数据,最后再通过 Proxy 将数据返回给页面 JS 使用。

经过的访问流程就是: 域 A 下 JS-- à 域 A 下 Proxy--- à 域 B 下的链接

 

例子:

第一步:

 

  域 A:  http://Jipiao.taobao.com/test.htm     页面上 javascript 脚本

 

[javascript] view plaincopyprint?
  1.    <mce:script type=”text/javascript”><!--  
  2.    
  3.   
  4.     Var sUrl=” http://Jipiao.taobao.com/proxy.do ”; // 本域下代理地址   
  5.   
  6. var callback =  
  7. {  
  8.    success: function(res) {   alert(res.responseText);   },   
  9.    failure: function(res) {  alert(‘failure’);},   
  10.    argument:{}   
  11. }   
  12.   
  13.   YAHOO.util.Connect.asyncRequest(’GET’, sUrl, callback, null);     
  14.   
  15.     
  16. // --></mce:script>   

 

第二步:

Proxy 程序 ( 这里假定是一个 servlet) :

 

  

[java] view plaincopyprint?
  1. Public class Proxy extends …….{   
  2.   
  3. ..doGet(……..){   
  4.   
  5. HttpClient  client=……;   
  6.   
  7. GetMethod get= new   GetMethod(" www.baidu.com/xxxxx.do ") ;// 访问域 B 的链接   
  8.   
  9. int statusCode = client.executeMethod( get );   
  10.   
  11. if (statusCode != HttpStatus.SC_OK) {   
  12.   
  13.    
  14.     
  15.    
  16. byte[] responseBody = get.getResponseBody();  
  17.   
  18. String res=new String(responseBody);  
  19.   
  20. Httpresponse.getWriter().write(res);//  
  21. 将数据返回给域  
  22. A  
  23.   
  24.   
  25.     }   
  26.   
  27. }   
  28.   
  29. }   
  

<!--[if !supportLists]-->② <!--[endif]-->  Script 标签 : 域 A 页面 http://Jipiao.taobao.com/test.htm 的 head 中写一个空的 Script 标签

[xhtml] view plaincopyprint?
  1. <html>   
  2.   
  3.   <head>   
  4.   
  5.   <mce:script id=”remoteScript” type=”text/javascript” src="””" mce_src="””" /><!--  
  6.    
  7.   
  8.   <head>   
  9.   
  10.   <body>   
  11.   
  12. <script type=”text/javascript” >   
  13.   
  14.   Var remoteScript=document.getElementById(‘remoteScript’);   
  15.   
  16.   remoteScript.src=” www.baidu.com/xxxxx.do”;// 域 B 的链接   
  17.   
  18.   alert(remote.test);// 使用域 B 返回的 JSON 数据   
  19.   
  20.   alert(f[0]);   
  21. // --></mce:script>   
  22.   
  23.   </body>   
  24.   
  25. </html>   

 

注意:这种方案要求域 B 返回的数据必须是合法的 JSON 格式或者如 JS 文件的格式。

 

域 B 返回的数据格式如下:

[javascript] view plaincopyprint?
  1. Var remote={test:’hello’};   
  2.   
  3. Var f=[‘2,1];   
  4.   
  5. {“test”:"hello","arrays":[2,1]}  

 

 

对于基于同一父域的子域之间页面的访问这一类情况,还有第三种方式: 
    ③ 隐藏 iframe: 即域 A jipiao.taobao.com/yyyy.htm 的页面上写一个隐藏的 iframe ,

[xhtml] view plaincopyprint?
  1. <html>   
  2.   
  3.   <head>   
  4.   
  5. <head>   
  6.   
  7.   <body>   
  8.   
  9. <mce:script type=”text/javascript” ><!--  
  10.    
  11.   
  12.   Document.domain=”taobao.com”;   
  13.   
  14.   Var remoteHtml=document.getElementById(“remoteHtml”);   
  15.   
  16. remoteHtml.src=”promotion.taobao.com/xxxx.htm”;// 这里访问域 B 的链接   
  17.   
  18. var document=remoteHtml.ContentDocument;   
  19.   
  20.   …// 这里就可以使用 document 来操作域 B 中页面 xxx.htm 的数据了   
  21. // --></mce:script>   
  22.   
  23. <iframe id=”remoteHtml” src="””" mce_src="””"   style="”diapay:none”/" mce_style="”diapay:none”/">   
  24.   
  25.   </body>   
  26.   
  27. </html>   
  

这里 promotion.taobao.com/xxxx.htm 页面也需要设置 document.domain="taobao.com" , 这种方法才能奏效。之所以这种 iframe 的方法不适合不同父域之间的跨域,是因为设置 document.domain 只能设置为自己的父域,而不是能设置为其他域,例如 :jiapiao.taobao.com 只能设置document.domain=”taobao.com” ,而不是是 document.domain=”baidu.com”

 

优缺点比较:

  这里列举的三种方案各有优缺点:

  Proxy 方案优点是可以适用用于几乎所有的跨域访问,而且只需要要一个域中进行开发,另一个域可以提供任何类型格式的数据。缺点是这种方案经过了中间 Proxy ,所以延迟可能稍微大一点,并且会加重本域服务器的负荷,开发工作量也稍微大一点。

 

  Script 标签的方案可以说是非常简单的,不用几行代码就搞定了事,不过它对返回的数据格式要求有点严格,只能是 Json 格式数据 , 如果是其他格式的数据,那么这种方法就无能为力了。

 

隐藏 iframe 方式也很简单,它可以处理任何返回的数据格式,但它只适用在具有同一个父域下的跨域请求上,并且要求其他域得配合开发,即需要设置 document.domain

 

推荐阅读文章:http://www.cnblogs.com/passer/archive/2008/07/16/1243811.html 

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

补充:Script标签标签方法最好要求服务端返回的json需要有句柄,即 如 json={...} 什么的。因为客户端需要使用这个句柄来引用。如果没有,客户端JS只有采用 var json=eval(jsonStr)方式来执行,效率不是很高。还有一种形式就是客户端传入要回调的方法。例如 xxxx.do?callbackApi=ca

服务端接收到callbackApi参数后,将json包装在ca中,如:ca({.....});

客户端定义回调函数就可以访问了。

   function ca(json){

         .......

   }

不论如何,这种方法对于服务端都有一定耦合。


0 0
原创粉丝点击