Javascript AngularJS 资源获取失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11598097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:28:33  来源:igfitidea点击:

AngularJS Failed Resource GET

javascriptjsonangularjs

提问by matsko

Does anyone know how you can check to see that a resource failed to be fetched in AngularJS?

有谁知道如何检查以查看在 AngularJS 中获取资源失败吗?

For example:

例如:

//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
    }
});

Any ideas?

有任何想法吗?

回答by Gloopy

An additional callback function after your first callback function should fire when there is an error. Taken from the docsand group post:

当出现错误时,第一个回调函数之后的附加回调函数应该会触发。取自文档和小组帖子

$scope.word = Word.get({ id : $routeParams.id }, function() {
    //good code
}, function(response) {
    //404 or bad
    if(response.status === 404) {
    }
});
  • HTTP GET "class" actions: Resource.action([parameters], [success], [error])
  • non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
  • non-GET instance actions: instance.$action([parameters], [success], [error])
  • HTTP GET“类”操作:Resource.action([parameters], [success], [error])
  • 非 GET“类”操作:Resource.action([parameters], postData, [success], [error])
  • 非 GET 实例操作: instance.$action([parameters], [success], [error])

回答by Rodrigo Chiong

Just to answer @Adio 's question too.

也只是为了回答@Adio 的问题。

The second callback will be called when any http response code is considered to be an error by AngularJS (only response codes in [200, 300] are considered success codes). So you can have a general error handling function and don't care about the specific error. The if statement there can be used to do different actions depending on the error code, but it's not mandatory.

当任何 http 响应代码被 AngularJS 认为是错误时,第二个回调将被调用(只有 [200, 300] 中的响应代码被认为是成功代码)。所以你可以有一个通用的错误处理功能,而不用关心具体的错误。那里的 if 语句可用于根据错误代码执行不同的操作,但这不是强制性的。

回答by Anavar Bharmal

This is just to inform.

这只是为了通知。

From angular 1.6.x, success and failure is deprecated. So please now follow the then and catch on behalf of success and failure.

从 angular 1.6.x 开始,成功和失败已被弃用。所以现在请按照 then 和 catch 代表成功和失败。

So, the above code look like in angular 1.6.x is as below:

因此,上面的代码在 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
});