How to show connection errors in HTML with a JSON?

2

I was developing HTML that accessed a JSON file. When, out of nowhere, the internet dropped from my house and I ended up thinking about it:

  • Is there a way to get a connection error and warn the user of this error?

For example, if I give an error, type ERR_CONNECTION_TIMED_OUT , how would I send a alert warning the user who gave time-out on the connection?

    
asked by anonymous 27.02.2017 / 13:25

1 answer

1

You can find out what type of error was given by accessing textStatus from error: function(jqXHR, textStatus, errorThrown);

I took this example from this post here: Determine if ajax error is a timeout

$.ajax({
    url: "/ajax_json_echo/",
    type: "GET",
    dataType: "json",
    timeout: 1000,
    success: function(response) { alert(response); },
    error: function(x, t, m) {
        if(t==="timeout") {
            alert("got timeout");
        } else {
            alert(t);
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
27.02.2017 / 18:21