【问题标题】:Catch errors with angularjs and restangular使用 angularjs 和 restangular 捕获错误
【发布时间】:2014-03-03 11:06:12
【问题描述】:

我有这个代码:

MyService.one($routeParams.wuid).doGET('test').then(function(e){
    alert('ok');
}, function errorCallback(response){
    alert('error');
});

它调用我管理的 API,但只有 alert('ok') 有效,在 404 响应的情况下,不会调用回调我的控制台中有一个 angularjs 错误。

Error: c is undefined this.$get</e/A/v@http://localhost:8888/client/app/js/libs/restangular.min.js:8 Ad/e/k.promise.then/C@http://localhost:8888/client/app/js/libs/angular.min.js:92 Ad/g/<.then/<@http://localhost:8888/client/app/js/libs/angular.min.js:94 Bd/this.$get</h.prototype.$eval@http://localhost:8888/client/app/js/libs/angular.min.js:102 Bd/this.$get</h.prototype.$digest@http://localhost:8888/client/app/js/libs/angular.min.js:100 Bd/this.$get</h.prototype.$apply@http://localhost:8888/client/app/js/libs/angular.min.js:103 f@http://localhost:8888/client/app/js/libs/angular.min.js:67 E@http://localhost:8888/client/app/js/libs/angular.min.js:71 pd/</v.onreadystatechange@http://localhost:8888/client/app/js/libs/angular.min.js:72

我不明白,因为the official doc 说:

如何处理错误?

可以在 then 的第二个参数上检查错误。

Restangular.all("accounts").getList().then(function() {
console.log("一切正常"); }, function(response) { console.log("错误 带状态码", response.status); });

我哪里错了?

【问题讨论】:

  • 在我的代码中完美运行。创建一个显示错误的 plunker 或 fiddle,我相信有人会提供帮助。
  • 感谢您的回复。我发布了错误的代码,我已经更正了。它是 doGet() 而不是 get(),没有其他变化

标签: angularjs restangular


【解决方案1】:

我找到了方法here 问题来自我昨天开始但尚未完成的拦截器:

myApp.run(function($http, $rootScope, $location, sessionService){
    $rootScope.$on('$routeChangeStart', function(event, next, current){
        if(!sessionService.isLogged() && next.requireLogin){
            $location.path("/login");
        }

        if(sessionService.isLogged()){
            if(next.controller !== "LoginCtrl"){
                $http.defaults.headers.common['Authorization'] = 'Bearer ' + sessionService.getToken();
            }
        } 
    });

});

当我删除这个拦截器时,我可以看到我的警报('错误')。我想我必须修复/结束拦截器,一切都会正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2017-01-29
    • 2015-09-14
    相关资源
    最近更新 更多