javascript - AngularJS 资源获取失败

有谁知道如何在 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
    }
});

有什么想法吗?

最佳答案

当出现错误时,应在您的第一个回调函数之后触发一个额外的回调函数。取自 docs和组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])

https://stackoverflow.com/questions/11598097/

相关文章:

javascript - 如何在不解析的情况下在javascript中同步包含JSON数据?

json - Jackson:有没有办法将 POJO 直接序列化为 treemodel?

ruby-on-rails - 将文件发送到 Rails JSON API

json - 将 JSON 与 LogStash 一起使用

c# - 反序列化期间的 JSON.Net Ignore Property

json - 浏览器 JSON 插件

c# - 使 ASP.NET WCF 将字典转换为 JSON,省略 "Key"和 "Value"标记

json - 如何解析 JSON 文件?

java - 在spring-mvc中将json解析为java对象

javascript - 未捕获的类型错误 : Cannot read property 'prop