angular http interceptors 拦截器使用分享
拦截器
在开始创建拦截器之前,一定要了解 $q和延期承诺api
出于全局错误处理,身份验证或请求的任何同步或异步预处理或响应的后处理目的,希望能够在将请求移交给服务器之前拦截请求,并在将请求移交给服务器之前将响应拦截发起这些请求的应用程序代码-拦截器利用promise api满足同步和异步预处理的需求。
拦截器是$httpProvider通过将它们添加到$httpProvider.interceptors数组而向其注册的服务工厂。调用工厂并注入依赖项(如果指定),并返回拦截器。
有两种拦截器(和两种拒绝拦截器):
request:拦截器通过http config对象调用。该函数可以自由修改config对象或创建新对象。函数需要config直接返回对象,或者包含config或新config对象的Promise。requestError:当先前的拦截器抛出错误或被拒绝解决时,拦截器将被调用。response:拦截器通过httpresponse对象调用。该函数可以自由修改response对象或创建新对象。函数需要response直接返回对象,或者作为包含response或新response对象的承诺。responseError:当先前的拦截器抛出错误或被拒绝解决时,拦截器将被调用。
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
// optional method
'request': function(config) {
// do something on success
return config;
}, // optional method
'requestError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}, // optional method
'response': function(response) {
// do something on success
return response;
}, // optional method
'responseError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
}); $httpProvider.interceptors.push('myHttpInterceptor'); // alternatively, register the interceptor via an anonymous factory
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
return {
'request': function(config) {
// same as above
}, 'response': function(response) {
// same as above
}
};
});
此处有一个坑,在push时,提示未定义拦截器,因为$httpProvider在config 拦截器时,拦截器service 还不能找到,
可以将拦截器service 定义在config依赖的模块中使用。
angular http interceptors 拦截器使用分享的更多相关文章
- angular之interceptors拦截器
<!DOCTYPE html> <html ng-app="nickApp"> <head> <meta charset="UT ...
- (vue.js)axios interceptors 拦截器中添加headers 属性
(vue.js)axios interceptors 拦截器中添加headers 属性:http://www.codes51.com/itwd/4282111.html 问题: (vue.js)axi ...
- flume1.8 Interceptors拦截器(五)
1. Flume Interceptors Flume有能力修改/删除流程中的events.这是在拦截器(interceptor)的帮助下完成的.拦截器(Interceptors)是实现org.apa ...
- springMVC之mvc:interceptors拦截器的用法
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- springMVC <mvc:interceptors>拦截器的使用
首先在springMVC.xml配置如下代码 <!-- 拦截器 --> <mvc:interceptors> <bean class="com.base.Acc ...
- Interceptors - 拦截器
1.概述 Flume有能力在运行阶段修改/删除Event,这是通过拦截器(Interceptors)来实现的. 拦截器需要实现org.apache.flume.interceptor.Intercep ...
- vue interceptors(拦截器)
拦截器 顾名思义: 就是半路个您劫持, 拦截器 其实在项目和自己写demo中,总会遇到请求方面需要在请求头里面做判断或者添加一些东西, 这时候 vue 中应用中axios的 interceptors ...
- (转)Angular中的拦截器Interceptor
什么是拦截器? 异步操作 例子 Session 注入(请求拦截器) 时间戳(请求和响应拦截器) 请求恢复 (请求异常拦截) Session 恢复 (响应异常拦截器) 转之:http://my.osch ...
- ionic2+Angular 使用HttpInterceptorService拦截器 统一处理数据请求
sstep1:新建http-Interceptor.ts文件 import { Injectable } from '@angular/core'; import { HttpInterceptorS ...
随机推荐
- Densely semantically aligned person re-identification
Densely semantically aligned person re-identification https://arxiv.org/abs/1812.08967
- Android Studio 教程
Android Studio 超详细安装教程 http://dkylin.com/archives/2019/android-studio-installation.html Android Stud ...
- RocketMQ安装部署
一.简介RocketMQ RocektMQ是阿里巴巴在2012年开源的一个纯java.分布式.队列模型的第三代消息中间件,不仅在传统高频交易链路有着低延迟的出色表现,在实时计算等大数据领域也有着不错的 ...
- Mysql 查询一个月当前时间一个月之前的数据
sql: AND date_format(createDTM, MONTH),'%Y-%m-%d')
- WPF Button LinkButton 绑定多个值 Template
效果如下: 代码如下: <Button Click="InventoryDetail_OnClick" Template="{StaticResource Inve ...
- api-doc-php
主要功能: 根据接口注释自动生成接口文档 演示地址 [Gitee Pages:]http://itxq.gitee.io/api-doc-php 开源地址: [GigHub:]https://gith ...
- Laya一个莫名其妙的报错laya.pathfinding
版本2.2.0 莫名其妙,然后论坛查了下 把类库设置,不勾选这玩意就不报错了.
- Pi1-加电
作为理工男,怎么能不折腾?终于等到我4G版的树莓派4. 1.制作系统.新手当然上官网下载官方系统,img文件,写入tf卡.mac下的步骤请见<mac下制作树莓派系统盘>,一步步来就行.思路 ...
- 【数据集】FDDB-Face Detection Data Set and Benchmark
前言 参考 1. FDDB官网: 完
- Dockerfile语法梳理
Dockerfile语法 我们先来看一下上篇的 Dockerfile #获取base image FROM adoptopenjdk/openjdk8:latest #类似于执行 linux指令 RU ...