可能是经过的项目少,还没有经历过跨域问题,于是收集点跨域问题,提前做好准备。
javascript出于安全方面的考虑,是不允许跨域调用其他页面的对象的。
要是大家好好用自己的东西什么事情也没有恩,但是总想用别人的东东,用别人的东西手短,
要是在项目中引用的 AJAX 与 ifram ,安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦。这里把涉及到跨域的一些问题简单地整理一下:
跨域,简单地理解就是因为javascript同源策略的限制,a.com 域名下的js无法操作b.com或是c.a.com域名下的对象。更详细的说明可以看下表:
判断是否跨域的问题 一是域名 包括子域,二是端口。
URL | 说明 | 是否允许通信 |
http://www.kuqin.com/lab/a.js http://www.kuqin.com/script/b.js | 同一域名下不同文件夹 | 允许 |
http://www.kuqin.com/a.js http://www.kuqin.com/b.js | 同一域名下 | 允许 |
http://www.kuqin.com:8000/a.js http://www.kuqin.com/b.js | 同一域名,不同端口 | 不允许 |
http://www.kuqin.com/a.js https://www.kuqin.com/b.js | 同一域名,不同协议 | 不允许 |
http://www.kuqin.com/a.js http://70.32.92.74/b.js | 域名和域名对应ip | 不允许 |
http://www.kuqin.com/a.js http://script.kuqin.com/b.js | 主域相同,子域不同 | 不允许 |
http://www.ithao123.com/a.js http://www.kuqin.com/b.js | 不同域名 | 不允许 |
特别注意两点:
第一,如果是协议和端口造成的跨域问题“前台”是无能为力的,
第二:在跨域问题上,域仅仅是通过URL的首部来识别而不会去尝试判断相同的ip地址对应着两个域或两个域是否在同一个ip上。
解决它们之间跨域的方案有:
方案1:服务器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脚本:
view plaincopy to clipboardprint?
<mce:script type="text/javascript"><!--
Var sUrl="http://Jipiao.taobao.com/proxy.do"; //本域下代理地址
var callback =
{
success: function(res) { alert(res.responseText); },
failure: function(res) { alert('failure');},
argument:{}
}
YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
// --></mce:script>
<mce:script type="text/javascript"><!--
Var sUrl="http://Jipiao.taobao.com/proxy.do"; //本域下代理地址
var callback =
{
success: function(res) { alert(res.responseText); },
failure: function(res) { alert('failure');},
argument:{}
}
YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
// --></mce:script>
第二步:
完成域A服务端的Proxy程序(这里假定是一个servlet),伪码如下:
view plaincopy to clipboardprint?
Public class Proxy extends …….{
..doGet(……..){
HttpClient client=……;
GetMethod get=new GetMethod("www.baidu.com/xxxxx.do");//访问域B的链接
int statusCode = client.executeMethod(get);
if (statusCode != HttpStatus.SC_OK) {
byte[] responseBody = get.getResponseBody();
String res=new String(responseBody);
Httpresponse.getWriter().write(res);//将数据返回给域A
}
}
}
Public class Proxy extends …….{
..doGet(……..){
HttpClient client=……;
GetMethod get=new GetMethod("www.baidu.com/xxxxx.do");//访问域B的链接
int statusCode = client.executeMethod(get);
if (statusCode != HttpStatus.SC_OK) {
byte[] responseBody = get.getResponseBody();
String res=new String(responseBody);
Httpresponse.getWriter().write(res);//将数据返回给域A
}
}
}
方案2:通过Script标签:
在域A页面http://Jipiao.taobao.com/test.htm 的head中写一个空的Script标签:
view plaincopy to clipboardprint?
<html>
<head>
<mce:script id="remoteScript" type="text/javascript" src=""/><!--
<head>
<body>
<script type="text/javascript" >
Var remoteScript=document.getElementById("remoteScript");
remoteScript.src="www.baidu.com/xxxxx.do";//域B的链接
alert(remote.test);//使用域B返回的JSON数据
alert(f[0]);
// --></mce:script>
</body>
</html>
<html>
<head>
<mce:script id="remoteScript" type="text/javascript" src=""/><!--
<head>
<body>
<script type="text/javascript" >
Var remoteScript=document.getElementById("remoteScript");
remoteScript.src="www.baidu.com/xxxxx.do";//域B的链接
alert(remote.test);//使用域B返回的JSON数据
alert(f[0]);
// --></mce:script>
</body>
</html>
注意:这种方案要求域B返回的数据必须是合法的JSON格式或者如JS文件的格式;比如域B返回的数据格式如下:
Var remote={test:’hello’};
Var f=[2,1];
方案3:隐藏iframe、共享domain:
即域A页面http://jipiao.taobao.com/yyyy.htm 的页面上写一个隐藏的iframe:
view plaincopy to clipboardprint?
<html>
<head>
<head>
<body>
<mce:script type="text/javascript" ><!--
Document.domain="taobao.com";
Var remoteHtml=document.getElementById("remoteHtml");
remoteHtml.src="promotion.taobao.com/xxxx.htm";//这里访问域B的链接
var document=remoteHtml.ContentDocument; //这里就可以使用document来操作域B中页面xxx.htm的数据了
// --></mce:script>
<iframe id="remoteHtml" src="" style="diapay:none" mce_style="diapay:none"/>
</body>
</html>
<html>
<head>
<head>
<body>
<mce:script type="text/javascript" ><!--
Document.domain="taobao.com";
Var remoteHtml=document.getElementById("remoteHtml");
remoteHtml.src="promotion.taobao.com/xxxx.htm";//这里访问域B的链接
var document=remoteHtml.ContentDocument; //这里就可以使用document来操作域B中页面xxx.htm的数据了
// --></mce:script>
<iframe id="remoteHtml" src="" style="diapay:none" mce_style="diapay:none"/>
</body>
</html>
注意:这里http://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。