SaidbakR
SaidbakR

Reputation: 13544

Connection faliure to the server in jQuery Ajax error

I'm trying to identify an error during submitting Ajax request via jQuery when the connection to the server is failed. I load the page from the server, then disconnect the network connection. The following error handle in the Ajax jquery is used:

error: function(xhr, status, error){
          var errorMessage = xhr.status + ': ' + xhr.statusText
         alert('Error - ' + errorMessage);
          alert('Connection Error\nCould not connect to the server.');
          $("#loading").removeClass("show");
          $("#submit_btn").removeAttr("disabled");
    }

The first alert prints out:

Error - 0: error

I could not able to evaluate the returned errors values to identify the error cause is due to connection failure or something else!

Upvotes: 1

Views: 227

Answers (1)

Anurag Srivastava
Anurag Srivastava

Reputation: 14423

The docs say that an error code of 0 basically means the request could not be completed, while other errors could be due to request completing but some other error in the endpoint or with response.

So usually with the error code 0 you can assume that there was a network issue.

Upvotes: 1

Related Questions