(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 }
原文地址:https://www.cnblogs.com/hellome/p/4026969.html