jonathantneal / polyfill

A polyfill combinator
MIT License
738 stars 13 forks source link

Polyfill cross-domain XHRs #55

Closed danwdart closed 9 years ago

danwdart commented 10 years ago

In IE < 10 cross-domain XHRs need a different object loading (XDomainRequest, which doesn't work for sync calls) like this for instance:

if (-1 != navigator.userAgent.indexOf('MSIE') && -1 == navigator.userAgent.indexOf('MSIE 1')) { // IE < 10 var x = new XDomainRequest(); x.timeout = 2000; x.onload = function() { return success(x.responseText, scope); } x.onerror = x.ontimeout = function() { return failure(scope); } x.open('GET', url); x.send(); }

Is it possible that this could be integrated?

Cheers

jonathantneal commented 10 years ago

We could certainly do our best. Would you setup a working and failing example?

danwdart commented 10 years ago

Sure, so an example of XHR failing in ie9?

danwdart commented 10 years ago

This for example will error in <= IE9 and work in other browsers:

var x = new XMLHttpRequest(); x.open('GET', 'https://api.github.com/', true); x.onreadystatechange = function() { if (4 == x.readyState) { if (200 == x.status) { // Do what you want with x.responseText } else { // Do what you want with your failure callback } } }

however this will work in IE8/9:

var x = new XDomainRequest(); x.onload = function() { // Do what you want with x.responseText return success(x.responseText); } x.onerror = x.ontimeout = function() { // Do what you want with your failure callback } x.open('GET', 'https://api.github.com/'); x.send();

defunctzombie commented 10 years ago

I am -1 on polyfilling xhr. Too many complications IMO. On Feb 27, 2014 2:39 PM, "Dan Dart" notifications@github.com wrote:

This for example will error in <= IE9 and work in other browsers:

var x = new XMLHttpRequest(); x.open('GET', 'https://api.github.com/', true); x.onreadystatechange = function() { if (4 == x.readyState) { if (200 == x.status) { // Do what you want with x.responseText } else { // Do what you want with your failure callback } } }

however this will work in IE8/9:

var x = new XDomainRequest(); x.onload = function() { // Do what you want with x.responseText return success(x.responseText); } x.onerror = x.ontimeout = function() { // Do what you want with your failure callback } x.open('GET', 'https://api.github.com/'); x.send();

Reply to this email directly or view it on GitHubhttps://github.com/jonathantneal/polyfill/issues/55#issuecomment-36282021 .

jonathantneal commented 10 years ago

If @dandart’s code works just as well if not better than our current XMLHttpRequest polyfill, I’ll all for adding it.

paulirish commented 10 years ago

crossdomain XHR is not terribly common nor is it a piece of cake. you're already setting up CORS headers. asking the developer to bring their own IE polyfill seems reasonable.

triblondon commented 9 years ago

@dandart this thread seems suitably contentious that I haven't raised it against the new service. If you are still keen to have it please consider raising it yourself here: https://github.com/Financial-Times/polyfill-service