在XMLHttpRequest对象访问跨域资源的时候的一些被承认的跨域访问资源的方案叫javascript
(CORS)跨域资源共享方案。java
在ie8中,能够经过XDomainRequest进行CORS,而在其余的浏览器中能够经过XHR对象跨域
便可进行CORS。浏览器
代码取自javascript高级程序设计3版:url
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 }