NETWORK_ERR: XMLHttpRequest Exception 101

NETWORK_ERR: XMLHttpRequest Exception 101

本文关键字:Exception XMLHttpRequest ERR NETWORK      更新时间:2023-09-26

我在Chrome中有一个AJAX问题,给出以下错误:

Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101

这是我的代码:

function IO(filename) {
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    xmlhttp.open("GET", filename+"?random="+Math.floor(Math.random()*100000001), false);
    xmlhttp.send();
    if(xmlhttp.readyState==4)
        return xmlhttp.responseXML;
}

解决方法是将async参数设置为true:

xmlhttp.open("GET", filename+"?random="+Math.floor(Math.random()*100000001), true);

除了在获取没有正确标头的跨站点URL时发生之外,在通过XHR (AJAX)获取本地文件时也会发生此错误。显然,Chrome在跨站点安全措施上过于热心,没有意识到一个文件URL应该被视为与另一个文件URL相同。这是许多本地应用的问题,尤其是Jasmine(一个JavaScript测试框架)。

Chrome版本16.0.912.63仍然发生。

我不知道任何解决办法。解决方法是使用Firefox或任何其他浏览器来运行从file: URLs获取的应用程序。