angular http interceptors 拦截器使用分享

拦截器

在开始创建拦截器之前,一定要了解 $q和延期承诺api

出于全局错误处理,身份验证或请求的任何同步或异步预处理或响应的后处理目的,希望能够在将请求移交给服务器之前拦截请求,并在将请求移交给服务器之前将响应拦截发起这些请求的应用程序代码-拦截器利用promise api满足同步和异步预处理的需求。

拦截器是$httpProvider通过将它们添加到$httpProvider.interceptors数组而向其注册的服务工厂。调用工厂并注入依赖项(如果指定),并返回拦截器。

有两种拦截器(和两种拒绝拦截器):

  • request:拦截器通过http config对象调用。该函数可以自由修改config对象或创建新对象。函数需要config直接返回对象,或者包含config或新config对象的Promise。
  • requestError:当先前的拦截器抛出错误或被拒绝解决时,拦截器将被调用。
  • response:拦截器通过http response对象调用。该函数可以自由修改response对象或创建新对象。函数需要response直接返回对象,或者作为包含response或新response对象的承诺。
  • responseError:当先前的拦截器抛出错误或被拒绝解决时,拦截器将被调用。
 1 // register the interceptor as a service
 2 $provide.factory(‘myHttpInterceptor‘, function($q, dependency1, dependency2) {
 3   return {
 4     // optional method
 5     ‘request‘: function(config) {
 6       // do something on success
 7       return config;
 8     },
 9
10     // optional method
11    ‘requestError‘: function(rejection) {
12       // do something on error
13       if (canRecover(rejection)) {
14         return responseOrNewPromise
15       }
16       return $q.reject(rejection);
17     },
18
19
20
21     // optional method
22     ‘response‘: function(response) {
23       // do something on success
24       return response;
25     },
26
27     // optional method
28    ‘responseError‘: function(rejection) {
29       // do something on error
30       if (canRecover(rejection)) {
31         return responseOrNewPromise
32       }
33       return $q.reject(rejection);
34     }
35   };
36 });
37
38 $httpProvider.interceptors.push(‘myHttpInterceptor‘);
39
40
41 // alternatively, register the interceptor via an anonymous factory
42 $httpProvider.interceptors.push(function($q, dependency1, dependency2) {
43   return {
44    ‘request‘: function(config) {
45        // same as above
46     },
47
48     ‘response‘: function(response) {
49        // same as above
50     }
51   };
52 });

此处有一个坑,在push时,提示未定义拦截器,因为$httpProvider在config 拦截器时,拦截器service 还不能找到,

可以将拦截器service 定义在config依赖的模块中使用。

原文地址:https://www.cnblogs.com/erming/p/11827359.html

时间: 2024-08-04 08:53:37

angular http interceptors 拦截器使用分享的相关文章

angular之interceptors拦截器

<!DOCTYPE html> <html ng-app="nickApp"> <head> <meta charset="UTF-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title&g

9.Interceptors - 拦截器

1.概述 Flume有能力在运行阶段修改/删除Event,这是通过拦截器(Interceptors)来实现的. 拦截器需要实现org.apache.flume.interceptor.Interceptor接口. 拦截器可以修改或删除事件基于开发者在选择器中选择的任何条件. 拦截器采用了责任链模式,多个拦截器可以按指定顺序拦截. 一个拦截器返回的事件列表被传递给链中的下一个拦截器. 如果一个拦截器需要删除事件,它只需要在返回的事件集中不包含要删除的事件即可. 如果要删除所有事件,只需返回一个空列

springMVC之mvc:interceptors拦截器的用法

1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> <mvc:mapping path="/**" /> <mvc:exclude-mapping path="/static/**" /> <bean class="拦截器java代码路径" /> </mvc:

(转)Angular中的拦截器Interceptor

什么是拦截器? 异步操作 例子 Session 注入(请求拦截器) 时间戳(请求和响应拦截器) 请求恢复 (请求异常拦截) Session 恢复 (响应异常拦截器) 转之:http://my.oschina.net/ilivebox/blog/290881?p=1 原文:Interceptors in AngularJS and Useful Examples $httpAngularJS 的 $http 服务允许我们通过发送 HTTP 请求方式与后台进行通信.在某些情况下,我们希望可以俘获所有

angular 拦截器

介绍:$http service在Angular中用于简化与后台的交互过程,其本质上使用XMLHttpRequest或JSONP进行与后台的数据交互.在与后台的交互过程中,可能会对每条请求发送到Server之前进行预处理(如加入token),或者是在Server返回数据到达客户端还未被处理之前进行预处理(如将非JSON格式数据进行转换):当然还有可能对在请求和响应过程过发生的问题进行捕获处理.所有这些需求在开发中都非常常见,所以Angular为我们提供了$http拦截器,用来实现上述需求. An

Angular http 拦截器

Angular http的拦截器一般用来处理每个http都需要添加的参数或者是统一处理错误信息 Angular1.x的http拦截器处理: $httpProvider.interceptors.push(function ($q) { return { request: function (config) { var url = config.url; //这个token表示是在登录状态, 不要用在header中,options无法设置header if(TASApp["x-auth-token

vue拦截器Vue.http.interceptors.push

刚开始学vue,github上down了一个开源项目,看源代码的时候看到了这个地方: /** * @export * @param {any} request * @param {any} next * @returns */ import store from './vuex/store' // 全局错误处理,全局loading import { setLoading, setTip } from './vuex/actions/doc_actions' export default func

Mybatis那些事-拦截器(Plugin+Interceptor)

作者:yhjyumi的专栏 Mybatis的拦截器实现机制,使用的是JDK的InvocationHandler. 当我们调用ParameterHandler,ResultSetHandler,StatementHandler,Executor的对象的时候,实际上使用的是Plugin这个代理类的对象,这个类实现了InvocationHandler接口.接下来我们就知道了,在调用上述被代理类的方法的时候,就会执行Plugin的invoke方法.Plugin在invoke方法中根据@Intercept

转载 - Struts2 拦截器详细配置过程

出处:http://www.blogjava.net/zzzlyr/archive/2009/10/12/297998.html Struts2 拦截器详细配置过程 1:所有拦截器的超级接口Interceptor ,Action去实现这个接口; Interceptor 它其中有三个方法(init(),destroy() ,interceptor()): Init()方法:在服务器起动的时候加载一次,并且只加载一次; Destroy()方法:当拦截器销毁时执行的方法; Interceptor()方