但是IE早在IE8时就推出了 XDomainRequest 对象进行跨域操作,一直沿用到IE10才被取代掉。因此在IE8,IE9中应该使用 XDomainRequest (XDR)来实现。 XDR在创建时是通过 new XDomainRequest() 进行创建。其他操作和XHR有细微差别。比如open方法只有method和url两个参数,XDR只支持异步不支持同步操作。 , 地址为:http://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js 对象,而jQuery并不兼容XDomainRequest.. 4)针对此方法网络上有解决的插件 jQuery-ajaxTransport-XDomainRequest 3. the XDomainRequest object for IE8 and IE9 when using the $.ajax function in jQuery 1.5+.
在IE9和IE8浏览器中,I引入了 XDomainRequest 对象。XDomainRequest 对象允许 AJAX 应用程序在满足一定条件的时候,直接发起安全的跨域请求。 必须使用 HTTP 或 HTTPS 协议访问目标 URL 这一条很简单——因为 XDomainRequest 对象依赖于一个HTTP响应头来实现访问控制,XDomainRequest 对象要求目标 URL 符合 HTTP 或 HTTPS 协议,以便于 XDomainRequest 对象检验响应头。 这使得添加XDomainRequest对象的支持功能变得比原先困难得多。 了解了XDomainRequest的一些限制之后,来写一个方法来实现它 function fetchIe9(url, options = {}){ if (window.XDomainRequest
值得注意的是,ie8和ie9是通过XDomainRequest来进行CORS通信的。XDomainRequest同样支持get和post方法。对象详细内容请见参考资料。 XDomainRequest的另一个坑是,当发送POST请求的时候,无法设置Header,如 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded HTTP_RAW_POST_DATA, $output); echo json_encode($output); } CORS支持情况:Chrome 4 , Firefox 3.5 , IE 8~9(XDomainRequest 参考资料 浅谈跨域 HTML5安全:CORS(跨域资源共享)简介 JavaScript最全的10种跨域共享的方法 XDomainRequest object iframe跨域通信的通用解决方案-第二弹!
为了解决 IE 上 XMLHttpRequest 对象的问题,就需要使用 IE 8 中引入的 XDomainRequest 对象。XDomainRequest 对象的作用是发出跨域的 AJAX 请求。 这是 XDomainRequest 对象和 XMLHttpRequest 对象的最大不同,也是使用 XDomainRequest 对象来实现类似原生 EventSource 对象的基础。 在使用 XDomainRequest 对象打开与服务器端的连接之后,当服务器端有新的数据产生时,可以通过 XDomainRequest 对象的 onprogress 事件的处理方法来进行处理,对接收到的数据进行解析 不过由于 XDomainRequest 对象本来的目的是发出跨域 AJAX 请求,考虑到跨域访问的安全性问题,XDomainRequest 对象在使用时的限制也比较严格。 对象,了解 XDomainRequest 对象的 使用限制。
withCredentials" in xhr) { xhr.open(method, url, true); } else if (typeof XDomainRequest = "undefined") { xhr = new XDomainRequest(); xhr.open(method, url); </head> <body> <input type="button" value="获取数据" id="btn1"> </body> </html> 注意点: 1.上面代码兼容IE8,因为用了XDomainRequest
它判断浏览器是否支持XDomainRequest,而并未在后台返回的头加上"Access-Control-Allow-Origin","*"。
xhr.open(method,url,true); }else if(typeof XDomainRequest =="undefined"){ xhr=new XDomainRequest(); xhr.send(null); }; sendAjaxRequest() 这种方式需要获取相应对象的支持,对于IE,则是XDomainRequest
看第三个参数 xhr.open(method, url, true); } else if (typeof XDomainRequest ! = "undefined") { // 此对象是 IE 用来跨域请求的 xhr = new XDomainRequest(); xhr.open(method IE 使用的是 XDomainRequest。 我想,对于 95% 的读者来说,说到这里就够了,我就不往下说了,读者如果有需要补充的,请在评论区留言。
xhr.open(method, url, true); } else if (typeof XDomainRequest ! = "undefined") { // 针对IE xhr = new XDomainRequest(); xhr.open(method, url);
// Chrome, Firefox, Opera and Safari xhr.open(method, url, true); } else if (typeof XDomainRequest = "undefined") { // XDomainRequest对象,兼容IE xhr = new XDomainRequest(): xhr.open
// Chrome, Firefox, Opera and Safari xhr.open(method, url, true); } else if (typeof XDomainRequest = "undefined") { // XDomainRequest对象,兼容IE xhr = new XDomainRequest(): xhr.open
HttpSelfHost/OWIN 如何以桌面应用程序方式,宿主承载WebApi服务; ASP.NET WebAPI HttpSelfHost/OWIN 如何让服务端支持CROS跨越访问; IE10.0 以下浏览器通过XDomainRequest
浏览器兼容性 对于IE9和IE8,需要把XMLHttpRequest请求改为XDomainRequest,IE10开始支持XMLHttpRequest。
XDomainRequest (IE89专用) XDomainRequest是在IE8和IE9上的HTTP access control (CORS) 的实现,在IE10中被 包含CORS的XMLHttpRequest 该接口可以发送GET和POST请求 XDomainRequest为了确保安全构建,采用了多种方法。
缺点: IE8、9 只是部分支持 CORS(例如:不能发送Cookie); https://blogs.msdn.microsoft.com/ieinternals/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds
ie8&9(XDomainRequest),10+,chrom4 ,firefox3.5,safair4,opera12支持这种方式。
8.CORS:跨域资源共享,w3c标准,支持兼容ie8,(IE8和IE9需要使用XDomainRequest对象来支持CORS)。
XDR对象(XDomainRequest):调用open(),接收两个参数:请求类型和URL,再调用send(),只支持异步执行。只要响应有效就会触发load事件,如果失败,会触发error事件。
小贴士 IE10以上用XMLHttpRequest对象实现CORS; IE8,IE9用XDomainRequest支持CORS。 整个CORS跨域,是浏览器自动完成,不需要前端特殊处理。
IE中实现CORS:XDR(XDomainRequest),所有的XDR请求都是异步的,不能创建同步请求。其使用方法类似于XHR。 2. XHR是否支持CORS的简单方式,就是检测是否存在withCredentials属性 xhr.open(method, url, true); } else if (typeof XDomainRequest = "undefined"){ // IE XDR xhr = new XDomainRequest(); xhr.open(method, url); }