javascript - XMLHTTPRequest - Test for Connection instead of .send(null) failure - Stack Overflow

var xml = null;xml = new XMLHttpRequest();xml.open("get", who, false);xml.send(null);if ((xml.

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

I want set up this alert where if there is no internet connection, it will say so. However, the browser stops/hangs at xml.send(null) with NS_ERROR_FAILURE:Failure. How can set up the test properly for a connection?

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

I want set up this alert where if there is no internet connection, it will say so. However, the browser stops/hangs at xml.send(null) with NS_ERROR_FAILURE:Failure. How can set up the test properly for a connection?

Share Improve this question asked Oct 14, 2012 at 5:39 dmandman 11.1k25 gold badges116 silver badges217 bronze badges 1
  • you want to test internet connection or connection to webserver ? you should make any request to webserver if you want to check if connection is here, – zb' Commented Oct 14, 2012 at 5:49
Add a ment  | 

1 Answer 1

Reset to default 6

Try this instead.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745223504a4617354.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信