Ajax.Request does not work with CORS in IE8 / 9, fails

I have a question about handling CORS prototypes with IE 10 browsers. I noticed that calling Ajax.Request on the cross-source URL in IE9 will fail by checking the access logs for the server so that it understands that the request was never done.

After some reading, it seems that IE8 / 9 should use its own object XDomainRequestto enable CORS, however, the Prototype function Ajax.getTransportreturns an object XMLHttpRequest, which, as I understand it, does not allow CORS in IE8 / 9?

I found a patch for jQuery for the same problem that tells me that this could also be a problem with Prototype, I just can’t find anything posted about it.

I implemented a bit of a hacker fix:

if (window.XDomainRequest) {
    // Use Microsoft XDR for IE8 and IE9
    var xdr = new XDomainRequest();
    xdr.open('POST', url);
    xdr.onload = function () {
        var data = xdr.responseText.evalJSON();
        callback_handler(data.domain);
    };
    setTimeout(function(){
        xdr.send();
    }, 0);
} else {
    new Ajax.Request(url,
        {
            method: 'post',
            onSuccess: function(response)
            {
                var data = response.responseText.evalJSON();
                callback_handler(data.domain);
            }
        }
    );
};

... , - ( Prototype) ?

+4
1

Ajax.getTransport :

var Ajax = {
  getTransport: function() {
    return Try.these(
      function() {return new XMLHttpRequest()},
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;
  }
}

, getTransport . , polyfill, XDomainRequest .

var _getTransport = Ajax.getTransport;
Ajax.getTransport = function() {
    return Try.these(
        function() { return new XDomainRequest() },
        _getTransport
    ) || false;
};

- Ajax.Request, XDR , .

N.B. - , XDR .

0

Source: https://habr.com/ru/post/1533703/


All Articles