什么是跨域?
概念:只要协议、域名、端口有任何一个不同,都被当作是不同的域。
[url=http://www.a.com/a.js]http://www.a.com/a.js[/url]
[url=http://www.a.com/b.js]http://www.a.com/b.js[/url] 同一域名下 允许
[url=http://www.a.com/lab/a.js]http://www.a.com/lab/a.js[/url]
[url=http://www.a.com/script/b.js]http://www.a.com/script/b.js[/url] 同一域名下不同文件夹 允许
[url=http://www.a.com:8000/a.js]http://www.a.com:8000/a.js[/url]
[url=http://www.a.com/b.js]http://www.a.com/b.js[/url] 同一域名,不同端口 不允许
[url=http://www.a.com/a.js]http://www.a.com/a.js[/url]
[url=https://www.a.com/b.js]https://www.a.com/b.js[/url] 同一域名,不同协议 不允许
[url=http://www.a.com/a.js]http://www.a.com/a.js[/url]
[url=http://70.32.92.74/b.js]http://70.32.92.74/b.js[/url] 域名和域名对应ip 不允许
[url=http://www.a.com/a.js]http://www.a.com/a.js[/url]
[url=http://script.a.com/b.js]http://script.a.com/b.js[/url] 主域相同,子域不同 不允许
[url=http://www.a.com/a.js]http://www.a.com/a.js[/url]
[url=http://a.com/b.js]http://a.com/b.js[/url] 同一域名,不同二级域名(同上) 不允许(cookie这种情况下也不允许访问)
[url=http://www.cnblogs.com/a.js]http://www.cnblogs.com/a.js[/url]
[url=http://www.a.com/b.js]http://www.a.com/b.js[/url] 不同域名 不允许
对于端口和协议的不同,只能通过后台来解决。
跨域资源共享(CORS)
CROS(Cross-Origin Resource Sharing)跨域资源共享,定义了必须在访问跨域资源时,浏览器与服务器应该如何沟通。CROS背后的基本思想就是使用自定义的HTTP头部让浏览器与服务器进行沟通,从而决定请求或响应是应该成功还是失败。
[url=http://segmentfault.com/u/trigkit4/",true]http://segmentfault.com/u/trigkit4/",true[/url]);
xhr.send();
</script>
代码与之前的区别就在于相对路径换成了其他域的绝对路径,也就是你要跨域访问的接口地址。
服务器端对于CORS的支持,主要就是通过设置Access-Control-Allow-Origin来进行的。如果浏览器检测到相应的设置,就可以允许Ajax进行跨域的访问。
要解决跨域的问题,我们可以使用以下几种方法:
通过jsonp跨域
现在问题来了?什么是jsonp?维基百科的定义是:JSONP(JSON with Padding)是资料格式 JSON 的一种“使用模式”,可以让网页从别的网域要资料。
JSONP也叫填充式JSON,是应用JSON的一种新方法,只不过是被包含在函数调用中的JSON,例如:
[url=http://example.com/data.php?callback=dosomething"></script]http://example.com/data.php?callback=dosomething"></script[/url]>
js文件载入成功后会执行我们在url参数中指定的函数,并且会把我们需要的json数据作为参数传入。所以jsonp是需要服务器端的页面进行相应的配合的。
[url=http://www.example.com/a.html]http://www.example.com/a.html[/url] , 在这个页面里面有一个iframe,它的src是[url=http://example.com/b.html]http://example.com/b.html[/url], 很显然,这个页面与它里面的iframe框架是不同域的,所以我们是无法通过在页面中书写js代码来获取iframe中的东西的:
[url=http://example.com/b.html]http://example.com/b.html[/url]" onload = "test()"></iframe>
这个时候,document.domain就可以派上用场了,我们只要把[url=http://www.example.com/a.html]http://www.example.com/a.html[/url] 和 [url=http://example.com/b.html]http://example.com/b.html[/url]这两个页面的document.domain都设成相同的域名就可以了。但要注意的是,document.domain的设置是有限制的,我们只能把document.domain设置成自身或更高一级的父域,且主域必须相同。
1.在页面 [url=http://www.example.com/a.html]http://www.example.com/a.html[/url] 中设置document.domain:
[url=http://example.com/b.html]http://example.com/b.html[/url]" onload = "test()"></iframe>
<script type="text/javascript">
document.domain = 'example.com';//设置成主域
function test(){
alert(document.getElementById('iframe').contentWindow);//contentWindow 可取得子窗口的 window 对象
}
</script>
2.在页面 [url=http://example.com/b.html]http://example.com/b.html[/url] 中也设置document.domain:
<script type="text/javascript">
document.domain = 'example.com';//在iframe载入这个页面也设置document.domain,使之与主页面的document.domain相同
</script>
修改document.domain的方法只适用于不同子域的框架间的交互。