有人知道如何检查在AngularJS中获取资源失败的情况吗?
例如:
//this is valid syntax
$scope.word = Word.get({ id : $routeParams.id },function() {
//this is valid, but won't be fired if the HTTP response is 404 or any other http-error code
});
//this is something along the lines of what I want to have
//(NOTE THAT THIS IS INVALID AND DOESN'T EXIST)
$scope.word = Word.get({ id : $routeParams.id },{
success : function() {
//good
},
failure : function() {
//404 or bad
}
});有什么想法吗?
发布于 2012-07-22 17:39:49
当出现错误时,在第一个回调函数之后应该会触发一个额外的回调函数。取自docs和组post
$scope.word = Word.get({ id : $routeParams.id }, function() {
//good code
}, function(response) {
//404 or bad
if(response.status === 404) {
}
});(parameters,success,error)
发布于 2013-02-15 17:37:24
也是为了回答@Adio的问题。
当任何http响应码被AngularJS认为是错误时,将调用第二个回调(只有200,300中的响应码被认为是成功码)。所以你可以有一个通用的错误处理函数,而不需要关心具体的错误。那里的if语句可用于根据错误代码执行不同的操作,但这不是必需的。
发布于 2017-01-05 13:43:26
这只是一个通知。
从angular 1.6.x开始,不推荐使用success和failure。所以现在请跟随then,捕捉代表成功和失败的信息。
因此,上面的代码在angular 1.6.x中如下所示:
$scope.word = Word.get({ id : $routeParams.id }).then(=> () {
//this is valid, but won't be fired if the HTTP response is 404 or any other http-error code
}).catch(=> () {
// error related code goes here
});https://stackoverflow.com/questions/11598097
复制相似问题