(CORS)跨域资源共享方案

在XMLHttpRequest对象访问跨域资源的时候的一些被认可的跨域访问资源的方案叫

(CORS)跨域资源共享方案。

在ie8中,可以通过XDomainRequest进行CORS,而在其他的浏览器中可以通过XHR对象

即可进行CORS。

代码取自javascript高级程序设计3版:

 1 function aCORSRequest(method,url){
 2     
 3     var xhr = new XMLHttpRequest();
 4     
 5     if('withCredentials' in xhr){
 6         //准备请求
 7         xhr.open(method,url,true);    
 8     }else if(typeof XDomainRequest != 'undefined'){
 9         
10         xhr = new XDomainRequest();
11         
12         xhr.open(method,url);
13         
14     }else{
15         xhr = null;    
16     }
17     
18     return xhr;
19     
20     
21 }

 

posted @ 2014-10-15 18:02  宇宙第一小菜鸟  阅读(176)  评论(0编辑  收藏  举报