【问题标题】:Angular $http not handling errorAngular $http 未处理错误
【发布时间】:2016-07-12 11:38:40
【问题描述】:

我的 Angular 应用无法处理 $http 调用中的错误。

我有一个$httpProvider,我按照文档的要求在errorResponse 中返回$q.reject(response)。在控制台中,角度只是放置angular.min.js:99 GET http://localhost:8080/my/api 500 (Internal Server Error)

代码

console.log('before');
$http.get('/my/api', function(response){
   console.log('ok');
   console.log(response);
}, function(response){
    console.log('not ok');
    console.log(response)
});
console.log('after');

我只收到“之前”、“之后”和上面的信息。

此外,在网络选项卡中,我从服务器获得预期响应,状态为 500,正文中包含 json 内容。

除了 $httpProvider 的 errorResponse 没有返回 $q.reject(),可能是什么问题?

【问题讨论】:

    标签: angularjs asynchronous promise angular-promise angularjs-http


    【解决方案1】:

    你有语法错误,你应该使用.then函数,然后给它们加上成功和错误回调。

    代码

    $http.get('/my/api').then(function(response){
       console.log('ok');
       console.log(response);
    }, function(response){
        console.log('not ok');
        console.log(response)
    });
    

    我对您的代码/问题的理解是,您期望执行error 回调函数,但它没有这样做。如果我错过了什么,那么请在 cmets 中提出同样的问题。

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 2017-12-16
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      相关资源
      最近更新 更多