Reputation: 211
I'm making a POST request to a TOMCAT server using AngularJS. Below is a snippet of that code.
$http(<myRequest>).then(function(response) {
$scope.info = response.data;
}, function(response) {
$scope.error = response.data
});
This works fine as long as the server is up and running. In case of any error on the response, it populates the $scope.error
variable with the error message. However, if the server is down, when I click the link that makes this request nothing is shown on the page, I can only see the error on the console of the browser.
How do I show this error on the page? I want to somehow notify the user that the server is down.
Upvotes: 0
Views: 1286
Reputation: 1838
Regarding the docs:
A response status code between 200 and 299 is considered a success status and will result in the success callback being called.
That said, if your error handler gets called you may check for the status
property of the response object. Values in the 400 or 500 range indicate problems that will prevent the server from processing your request. If you are configuring the server yourself, you are able to make up your own error codes and interpret them client side.
$http(<myRequest>).then(function success (response) {
$scope.info = response.data;
}, function error (response) {
if (response.status === <server is down http status>) {
//do something here
$scope.error = "server is down msg";
}
else {
$scope.error = response.data;
}
});
I tried to post data to a non-existent server which gave me the status code -1
. If your server is really down, you may have to check for this value.
Edit: There's also a statusText
property within your response that could be helpful :)
Upvotes: 1