XMLHttpRequest异常处理未执行

时间:2015-12-17 04:09:32

标签: javascript google-chrome-extension

我正在撰写Chrome扩展程序。在后台页面脚本中我有:

function httpGetAsync(theUrl, callback)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
               callback(xmlHttp.responseText);
            } else {
                console.log("Error: ", xmlHttp.statusText);
            }
    };
    xmlHttp.open('GET', theUrl, true); // true for asynchronous 
    try {
        xmlHttp.send(null);
    } catch (err) {
        console.log("Caught Error");
    }
}

当URL的服务器关闭时,我在JavaScript控制台'net​​ :: ERR_CONNECTION_REFUSED'上得到一个例外。我想捕获异常,因此它不会出现在控制台上,而不仅仅是打印出“错误:”。请注意,在“重复”问题中没有回答这个问题。

0 个答案:

没有答案