Response code you mentioned as a reference :
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script><script>angular.module('app',[]);angular.module('app').controller('MyCtrl',functionMyCtrl($http,$scope){$scope.busca=function(){$http.get('http://api.postmon.com.br/cep/'+$scope.cep).success(function(local){$scope.local_encontrado=local;console.log(local);});};$scope.enter=function(e){if(e.keyCode==13){$scope.busca();};};});</script>
Noticethattheexampleusesonlythesuccess
method,thatis,whenitreturnsnoerror,andtheresponsestatusis2**
,usually200
.Sowhat'smissing?Itisnecessarytomentionanothermethodcallederror
,withtheintentionoftreatingwhenacertainerroroccurs,liketheoneyoumentioned,seehowthecodeis:
Codechangehandlingerrors:
<linkrel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script><script>angular.module('app',[]);angular.module('app').controller('MyCtrl',functionMyCtrl($http,$scope){$scope.busca=function(){$http.get('http://api.postmon.com.br/cep/'+$scope.cep).success(function(local){$scope.local_encontrado=local;console.log(local);}).error(function(e){alert("Erro ao carregar CEP");
});
};
$scope.enter = function(e){
if(e.keyCode == 13){
$scope.busca();
};
};
});
</script>
In the body of the code I insert the function error
so that when a certain error occurs, it shows an alert for the user to know what happened, this method will usually be called when a status of an HTTP response is 4**
or 5**
.
This will treat the errors as the error you mentioned, but if you do not find a particular zip code refer to the API documentation that you are using to verify your return. Even if you do not find the zip code, it will fall within success
to handle this information, it will not fall within error
because it was a successful request, right?
References :
link
link