javascript XDomainRequest (CORS) for XML 在 IE8/IE9 中导致“访问被拒绝”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20624476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:54:21  来源:igfitidea点击:

XDomainRequest (CORS) for XML causing "Access is denied" error in IE8 / IE9

javascriptajaxinternet-explorer-8xmlhttprequestcross-domain

提问by tripRev

Apologies if this appears to be a duplicate but I cannot see a clear answer to any of the similar questions.

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

When trying to do a CORS request for some XML I continually get an "Access is denied" JS error from IE8.

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

My code is adapted from this example:

我的代码改编自这个例子:

// 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();
}

from http://www.html5rocks.com/en/tutorials/cors/

来自http://www.html5rocks.com/en/tutorials/cors/

This shouldwork in IE8 using XDomainRequest, and when I load the example page and click "Run sample" on the html5rocks page, it works in IE8. However, as soon as I copy the code to my own page and run, I get the "Access is denied" error on the xhr.open() line inside XDomainRequest.

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

This one has me really baffled - the server is definitely set up correctly so it's something to do with the frontend. Thanks in advance to anyone who can help!

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

回答by tripRev

OK, the problem was down to weirdnesses in IE8 & 9 which were solved with a few suggestions from this article: http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/(mainly setting some blank handler functions and wrapping the .send() in a 0 timeout).

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

Here's my final code which works in ie8/9/10/11 & FF/Chrome:

这是我在 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);

};