2
    var xml = null;
    xml = new XMLHttpRequest();
    xml.open("get", who, false);
            xml.send(null);
    if ((xml.status >= 200 && xml.status <= 300) || xml.status == 304) {
    var hi = xml.responseText;
    } else {
    alert("No Internet Connection! You will have to enter information by hand");
    };

我想设置这个警报,如果没有互联网连接,它会这样说。但是,浏览器在 xml.send(null) 处停止/挂起,并带有 NS_ERROR_FAILURE:Failure。如何为连接正确设置测试?

4

1 回答 1

6

试试这个。

function check() {
    var z, xml = null;
    xml = new XMLHttpRequest();
    xml.open("get", who, false);
    try {
        xml.send(null);
    } catch(z) {
        alert("Network failure");
        return;
    }
    if ((xml.status >= 200 && xml.status <= 300) || xml.status == 304) {
        var hi = xml.responseText;
    } else {
        alert("No Internet Connection! You will have to enter information by hand");
    }
}
于 2012-10-14T12:10:22.313 回答