Angular $ http没有处理错误

Max*_*hen 1 asynchronous promise angularjs angularjs-http angular-promise

我的角度应用程序不处理$ http调用中的错误.

我有一个$httpProvider$q.reject(response)在errorResponse中返回的文档所需的位置.在控制台中角度刚刚放置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');
Run Code Online (Sandbox Code Playgroud)

我只是得到'之前','之后'和上面的信息.

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

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

Pan*_*kar 5

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

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

我的代码/问题所理解的是,您希望获得error回调函数来执行,但它并没有这样做.如果我错过了什么,那么请在评论中提问.