XMLHTTPRequest - 测试连接而不是.send(null)失败

时间:2012-10-14 05:39:56

标签: javascript xmlhttprequest

    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:失败。如何为连接正确设置测试?

1 个答案:

答案 0 :(得分:5)

试试这个。

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");
    }
}