How to show connection errors in Htmls with a JSON?

Asked

Viewed 190 times

2

I was developing an HTML that accessed a JSON file. When, out of nowhere, the internet fell off my house and I ended up thinking about this question:

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

For example, if an error occurs, like the ERR_CONNECTION_TIMED_OUT, how I would send a alert warning the user who gave time-out in connection?

1 answer

1


You can find out what kind of error was given by accessing the textStatus of 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>

  • Okay. It worked great here. Thanks for the help! :)

  • @Gabrielpolidoro anything! I’m glad to have helped, you can flag her as response?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.