XDomainRequest(CORS)的XML引起"访问被拒绝"在IE8 / IE9错误被拒、错误、XML、XDomainRequest

2023-09-10 16:34:43 作者:╱羞答答的玫瑰静悄悄的开

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

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

在试图做一个CORS请求某些XML我不断收到访问被拒绝JS错误与IE8。

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

我的code是改编自这个例子:

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

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

本的应该的工作在IE8中使用XDomainRequest,当我打开示例页面,点击HTML5ROCKS页上的运行样品,它工作在IE8。然而,当我复制code到自己的网页和运行,我得到了访问被拒绝的错误在里面XDomainRequest的xhr.open()行。

This should work 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.

这其中有我真的很困惑 - 服务器肯定是设置正确以便它的东西做的前端。在此先感谢任何人谁可以帮助!

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!

推荐答案

确定,问题是下降到IE8和放大器weirdnesses; 9人解决了从这篇文章的几个建议:的http://cy$p$pssnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/ (主要是设置一些空白的处理功能和包装的。发送()在0超时)。

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

下面是我的最后code在IE8 / 9/10/11安培其中工程; FF / Chrome浏览器:

Here's my final code which works in 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);

};
 
精彩推荐
图片推荐