I am trying to catch an angular resource http error code (! = 200). My service where I defined the resources: (ApiService.js)
.factory('ApiService', function($resource, $http, localStorageService, CONFIG) {
var base_api_url = api_url = CONFIG.api_url, api_version_prefix = CONFIG.api_version_prefix;
return {
userDevices: $resource(api_url+'/requestRegistration/userDevices/:action', {}, {
registerDevice: {
method: 'POST',
params: {
action: ''
}
},
verify: {
method: 'POST',
params: {
action: 'verify'
}
},
}
}
});
My controller code:
.controller('LoginCtrl', function(CONFIG, $scope, $state, $ionicPlatform, $ionicPopup, ApiService) {
$scope.data = {
username: null
};
$scope.registerDevice = function() {
if($scope.data.username) {
var authenticationResponse = ApiService.userDevices.registerDevice({
username: $scope.data.username
});
authenticationResponse.$promise.then(function(result) {
console.log(result);
console.log('success!');
}, function(error){
console.log('error!');
});
}
};
});
When I send a request and receive a 400/503 response code, I believe that the function (error) code should be executed, but it is not.
Instead, my code is executed in $promise.then(function(result)(...), and I cannot detect the response code of the HTTP response.
So my questions are:
- Why is my error handling function not executing?
- How to determine HTTP response status codes?
dease source
share