Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch errors involving XmlHttpRequest?

I want to catch all errors with the help of TRY {} CATCH(){} when I send data to a server via XMLHttpRequest.

How can I receive all errors, such as net::ERR_INTERNET_DISCONNECTED, etc. ?

like image 380
Hit-or-miss Avatar asked Nov 05 '14 12:11

Hit-or-miss


Video Answer


1 Answers

Try catches didn't work for me. I personally ended up testing for response == "" and status == 0.

        var req = new XMLHttpRequest();
        req.open("post", VALIDATE_URL, true);
        req.onreadystatechange = function receiveResponse() {

            if (this.readyState == 4) {
                if (this.status == 200) {
                    console.log("We got a response : " + this.response);
                } else if (!isValid(this.response) && this.status == 0) {
                    console.log("The computer appears to be offline.");
                }
            }
        };
        req.send(payload);
        req = null;
like image 139
Francois Dermu Avatar answered Sep 19 '22 23:09

Francois Dermu