On my web application I perform a GET
mand to a remote HTTP WebAPI service
$http.get(url).then(function(data) { do_something(); });
everything works fine when the WebAPI returns some data, however the function doesn't seem to trigger when the WebAPI return a 404 error (no data to display). How can I set a callback for it?
On my web application I perform a GET
mand to a remote HTTP WebAPI service
$http.get(url).then(function(data) { do_something(); });
everything works fine when the WebAPI returns some data, however the function doesn't seem to trigger when the WebAPI return a 404 error (no data to display). How can I set a callback for it?
Share Improve this question asked Jun 1, 2016 at 9:24 Gianluca GhettiniGianluca Ghettini 11.7k24 gold badges99 silver badges168 bronze badges3 Answers
Reset to default 5$http.get(url).then(function(data) {
do_something();
}, function(err) {
// your error function
if (err.status == 404) {
do_something_else();
}
});
I think for the 404 responses, best thing is to show proper not found page. In angularjs you can intercept the response with $injector service. So you can create a service which look for 404 response and show 404 page.
angular.module('mymodule')
.service('APIInterceptor', function( $injector) {
return {
'request': function(config){
// request logic goes here.
},
'responseError' : function(response){
if (response.status === 404) {
$injector.get('$state').go('error_500');
}
return response;
}
};
});
$http
returns status code as the second argument.
$http.get(url)
.success(function(data, status) {
alert(status);
})
.error(function(data, status) {
alert('Error with status code: ' + status);
});
status
– {number}
– HTTP status code of the response.
However, if the status is an error status, such as 404, then the error block will be called
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742306506a4419043.html
评论列表(0条)