Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NETWORK_ERR: XMLHttpRequest Exception 101

I'm having an AJAX problem in Chrome, giving the following error:

Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101

This is my code:

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;
}
like image 455
Pawan Goswami Avatar asked Aug 06 '11 09:08

Pawan Goswami


2 Answers

The solution is setting the async parameter to true:

xmlhttp.open("GET", filename+"?random="+Math.floor(Math.random()*100000001), true);
like image 62
headmax Avatar answered Oct 08 '22 13:10

headmax


In addition to happening when fetching a cross-site URL without proper headers, this error occurs when fetching a local file via XHR (AJAX). Apparently Chrome is being overzealous with its cross-site security measures, not realizing that one file: URL should be considered the same site as another file: URL. This is a problem for many homegrown apps, especially Jasmine (a JavaScript testing framework).

Still happening as of Chrome version 16.0.912.63 .

I don't know any solution. Workaround is to use Firefox, or any other browser, to run apps served off of file: URLs.

like image 45
AlexChaffee Avatar answered Oct 08 '22 11:10

AlexChaffee