XDomainRequest (CORS) for XML 导致 IE8 / IE9 中"Access is denied"错误



抱歉,如果这似乎是重复的,但我看不到任何类似问题的明确答案。

当尝试对某些XML进行CORS请求时,我不断收到来自IE8的"访问被拒绝"JS错误。

我的代码改编自此示例:

// Create the XHR object.
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {
    // XHR for Chrome/Firefox/Opera/Safari.
    xhr.open(method, url, true);
  } else if (typeof XDomainRequest != "undefined") {
    // XDomainRequest for IE.
    xhr = new XDomainRequest();
    xhr.open(method, url);
  } else {
    // CORS not supported.
    xhr = null;
  }
  return xhr;
}
// Helper method to parse the title tag from the response.
function getTitle(text) {
  return text.match('<title>(.*)?</title>')[1];
}
// Make the actual CORS request.
function makeCorsRequest() {
  // All HTML5 Rocks properties support CORS.
  var url = 'http://updates.html5rocks.com';
  var xhr = createCORSRequest('GET', url);
  if (!xhr) {
    alert('CORS not supported');
    return;
  }
  // Response handlers.
  xhr.onload = function() {
    var text = xhr.responseText;
    var title = getTitle(text);
    alert('Response from CORS request to ' + url + ': ' + title);
  };
  xhr.onerror = function() {
    alert('Woops, there was an error making the request.');
  };
  xhr.send();
}

与 http://www.html5rocks.com/en/tutorials/cors/相比

应该在使用XDomainRequest的IE8中工作,当我加载示例页面并单击html5rocks页面上的"运行示例"时,它可以在IE8中工作。但是,一旦我将代码复制到我自己的页面并运行,我就会在 XDomainRequest 中的 xhr.open() 行上收到"访问被拒绝"错误。

这个让我真的很困惑 - 服务器肯定设置正确,所以这与前端有关。提前感谢任何可以提供帮助的人!

好的,问题归结为IE8和9中的奇怪之处,本文中的一些建议解决了这些怪异之处:http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/(主要是设置一些空白处理程序函数并将.send()包装在0超时中)。

这是我在ie8/9/10/11和FF/Chrome中工作的最终代码:

function doRequest(url) {
    // Create the XHR object.
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        // XHR for Chrome/Firefox/Opera/Safari.
        xhr.open('get', url, true);
    }else if(typeof XDomainRequest != "undefined") {
        // XDomainRequest for IE.
        xhr = new XDomainRequest();
        xhr.open('get', url);
    }else{
        // CORS not supported.
        xhr = null;
    };
    if (!xhr) {
        return;
    };
    // Response handlers.
    xhr.onload = function() {
        //do what you want with the response. Remember to use xhr.responseText for ie8 compatibility, because it doesn't have .responseXML
        if(xhr.responseXML) {
            xml = this.responseXML;
        }else if(xhr.responseText){
            xml = new ActiveXObject('Microsoft.XMLDOM');
            xml.loadXML(xhr.responseText);
        };
    };
    xhr.onerror = function() {
        //do what you want on error
    };
    //these blank handlers need to be set to fix ie9 http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/
    xhr.onprogress = function () { };
    xhr.ontimeout = function () { };
    //do it, wrapped in timeout to fix ie9
    setTimeout(function () {
                xhr.send();
            }, 0);
};

最新更新