用于XML的XDomainRequest(CORS)"访问被拒绝" IE8 / IE9中的错误

时间:2013-12-17 01:46:44

标签: javascript ajax internet-explorer-8 xmlhttprequest cross-domain

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

当尝试对某些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页面上的“Run sample”时,它可以在IE8中运行。但是,只要我将代码复制到我自己的页面并运行,我就会在XDomainRequest内的xhr.open()行上出现“访问被拒绝”错误。

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

1 个答案:

答案 0 :(得分:8)

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

这是我的最终代码,适用于ie8 / 9/10/11&amp; FF /铬:

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);

};
相关问题