CORS with jQuery and XDomainRequest in IE8 / 9

UPDATE: I highly recommend not investing in XDomainRequest, because it is a very poor implementation with many limitations. It basically only works for GET requests for servers other than ssl, so you can also use jsonp or something else.




I use CORS to invoke the cross-domain API, however Internet Explorer gives problems. CORS should be possible in IE8 and IE9 through an XDomainRequest object, however I cannot get the work to work.

JQuery refuses to provide native support for XDomainRequest, however several jQuery plugins are invited to add this support. This topic offers two such plugins: jQuery.XDomainRequest.js and the reported xdr.js. Afaik, plugins should automatically override jQuery.ajax behavior. I found another plugin here .

I posted small demo pages with the corresponding jQuery.XDomainRequest and xdr and jquery.ie.cors plugins that execute ajax requests on a CORS-enabled server. Pages work in Chrome and Firefox, however IE8 / 9 instantly throws a rejected resolved error (even before submitting a request). This MSDN message suggests adding another handler xhr.onprogress = function() {}; but I tried this and it doesn't work either.

Any clues what am I doing wrong? I also tested with IE8 now using the MS virtual server, but it has exactly the same problem.

Edit: OK, so I realized that part of the problem was that I used POST on top of HTTPS. XDomainRequest does not seem to allow CORS over HTTPS. I can switch to HTTP, but I really need POST.

Edit2: See this issue on github for a complete story. It turns out that when using HTTP POST xDomainRequest can encode the request body (arguments) only as text/plain . This pretty much makes it useless because everyone uses application/x-www-form-urlencoded or multipart/form-data .

+62
jquery internet-explorer-9 cors xdomainrequest
Jul 14 2018-12-12T00:
source share
3 answers

The POST method is supported, and to create a cross-domain https: // request for your calling page you also need to download via https. This is the best article I've found that explains these and other limitations of XDomainRequest in detail:

http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

+28
Aug 24 '12 at 11:24
source share

I wrote a proxy server that will elegantly downgrade to proxy if IE9 or less is used. You do not need to change your code at all if you are using ASP.NET.

The solution consists of two parts. The first is a jquery script that intercepts jQuery ajax processing. It will automatically call the web server if the crossDomain request is executed, and the browser does IE:

 $.ajaxPrefilter(function (options, originalOptions, jqXhr) { if (!window.CorsProxyUrl) { window.CorsProxyUrl = '/corsproxy/'; } // only proxy those requests // that are marked as crossDomain requests. if (!options.crossDomain) { return; } if (getIeVersion() && getIeVersion() < 10) { var url = options.url; options.beforeSend = function (request) { request.setRequestHeader("X-CorsProxy-Url", url); }; options.url = window.CorsProxyUrl; options.crossDomain = false; } }); 

On your web server, you should receive the request, get the value from the X-CorsProxy-Url http header and execute the HTTP request and finally return the result.

My blog post: http://blog.gauffin.org/2014/04/how-to-use-cors-requests-in-internet-explorer-9-and-below/

+5
Apr 24 '14 at 11:04
source share

For CORS queries in IE8 / 9, you can use the jQuery plugin jquery-transport-xdr

0
Oct. 06 '15 at 20:18
source share



All Articles