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

 手机用户2502919831 发布于 2023-02-10 00:40

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

当尝试对某些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('(.*)?')[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 个回答
  • 好吧,问题归结为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);
    
    };
    

    2023-02-10 01:09 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有