在Angularjs中处理$ http.get和$ http.post错误 [英] handling $http.get and $http.post errors in Angularjs

查看:407
本文介绍了在Angularjs中处理$ http.get和$ http.post错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计,



我使用 $ http.get $ http。发布遍及我的代码。对于如何以全球方式处理这些调用中发生的错误,我有点失落。
当前我在每次调用时都有 .success(doSomething).error(doSomething)



I我希望改变这一点,而不是简单地将错误显示在我的页面顶部。



我阅读了有关使用拦截器的 myapp.something 。但我绝对不明白如何实现这一点。



请协助 解决方案

响应拦截器在配置阶段的angularjs应用程序。您可以使用它们全局响应任何$ http请求。请注意,模板文件也使用$ http请求,因此您可能希望将拦截器中的某些请求过滤为您希望响应的请求。



良好的理解需要承诺模式才能成功使用响应拦截器。




$ b

  angular.module('services')

$ httpProvider.responseInterceptors.push('globalInterceptor');
})
//函数($ httpProvider){
//这里我们添加拦截器。这里我们定义了我们的拦截器
.factory('globalInterceptor',function($ q){
//当拦截器运行时,它传递一个promise对象
return function(promise){

//在拦截器中,我们返回由.then函数创建的另一个承诺。
return promise.then(function(response){
//如果响应成功,请在此处执行代码

//始终确保返回应用程序代码的响应以响应以及
返回响应;
},函数(response){
//如果响应失败,请在这里执行错误处理代码

//如果不能从中恢复,务必返回一个拒绝不知怎的,错误。
//这样,$ http请求的使用者将知道它的错误以及
return $ q.reject(response);
});
}
});


Folks,

I am using $http.get and $http.post all over my code. I am kind of lost as to how to handle errors that occur during these calls in a global fashion. Currently I have .success(doSomething).error(doSomething) on every call.

I would like to change this and instead simply display the error on top of my page.

I read about using interceptors with myapp.something. But I absolutely do not understand how to implement this.

Kindly assist

解决方案

Response interceptors are attached during the config phase of an angularjs app. You can use them to globally respond to any $http request. Be aware, template files also use $http request, so you may wish to filter some requests in your interceptor to only the ones you wish to respond to.

A good understanding of the promises pattern is needed to successfully use response interceptors.

Heres an example of how they are used:

angular.module('services')
    .config(function($httpProvider){
        //Here we're adding our interceptor.
        $httpProvider.responseInterceptors.push('globalInterceptor');
    })
    //Here we define our interceptor
    .factory('globalInterceptor', function($q){
        //When the interceptor runs, it is passed a promise object
        return function(promise){

            //In an interceptor, we return another promise created by the .then function.
            return promise.then(function(response){
                //Do your code here if the response was successful

                //Always be sure to return a response for your application code to react to as well
                return response;
            }, function(response){
                //Do your error handling code here if the response was unsuccessful

                //Be sure to return a reject if you cannot recover from the error somehow.
                //This way, the consumer of the $http request will know its an error as well
                return $q.reject(response);
            });
        }
    });

这篇关于在Angularjs中处理$ http.get和$ http.post错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆