angular 用拦截器统一处理http请求和响应 比如加token
想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}}
index.html里引入以下js:
angular.module('app.factorys',[])
.factory('httpInterceptor',['$q','$injector','$localStorage',function ($q,$injector,$localStorage) {
var httpInterceptor = {
'responseError' : function(response) {
// ......
return $q.reject(response);
},
'response' : function(response) {
if (response.status == ) {
// console.log('do something...');
}
return response || $q.when(response);
},
'request' : function(config) {
config.headers = config.headers || {};
if ($localStorage.token) {
config.headers.token = $localStorage.token;
// config.headers['X-Access-Token'] = $localStorage.token;
}; return config || $q.when(config); return config;
},
'requestError' : function(config){
// ......
return $q.reject(config);
}
};
return httpInterceptor;
}])
在app里注入factory后,在config里面配置
.config(['$httpProvider',function(){
$httpProvider.interceptors.push(httpInterceptor);
}])
如果你的代码并未做拆分,可以直接在config里面写拦截器
$httpProvider.interceptors.push(['$q','$injector','$localStorage',function ($q,$injector,$localStorage) {
var httpInterceptor = {
'responseError' : function(response) {
// todo...
return $q.reject(response);
},
'response' : function(response) {
if (response.status == ) {
// console.log('do something...');
}
return response || $q.when(response);
},
'request' : function(config) {
config.headers = config.headers || {};
if ($localStorage.token) {
config.headers.ut = $localStorage.token; //把你登录接口返回给你的token存到$localStorage里面,在这里取就好了
// config.headers['X-Access-Token'] = $localStorage.token;
}; return config || $q.when(config);
// return config;
},
'requestError' : function(config){
// todo...
return $q.reject(config);
}
};
return httpInterceptor;
}]);
angular 用拦截器统一处理http请求和响应 比如加token的更多相关文章
- ionic2+Angular 使用HttpInterceptorService拦截器 统一处理数据请求
sstep1:新建http-Interceptor.ts文件 import { Injectable } from '@angular/core'; import { HttpInterceptorS ...
- AngularJS 拦截器实现全局$http请求loading效果
日常项目开发中,当前端需要和后端进行数据交互时,为了友好的UI效果,一般都会在前端加个loading的状态提示(包括进度条或者icon显示),数据传输或交互完成之后,再隐藏/删除loading提示. ...
- axios封装,使用拦截器统一处理接口
1.项目路径下,引入axios.qs依赖 npm install axios npm install qs 2.在项目的src路径下新建一个commJs文件夹,在commJs文件夹里新建aps.js和 ...
- Struts2 在登录拦截器中对ajax请求的处理
前言: 由于ajax请求不像http请求,可以直接进行页面跳转,你返回的所有东西,ajax都只会识别为一个字符串. 之前尝试的方法是在拦截器中返回一个标识给ajax,然后再在每一个ajax请求成功之后 ...
- Okhttp拦截器统一异常处理并多次读取response.body().string()
参考:https://blog.csdn.net/a624806998/article/details/73863606 引言: 写这篇文章,因为在自己编写实现Http日志拦截器的时候,在拦截器中使用 ...
- AngularJS 用 Interceptors 来统一处理 HTTP 请求和响应
Web 开发中,除了数据操作之外,最频繁的就是发起和处理各种 HTTP 请求了,加上 HTTP 请求又是异步的,如果在每个请求中来单独捕获各种常规错误,处理各类自定义错误,那将会有大量的功能类似的代码 ...
- java拦截器与过滤器打印请求url与参数
HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServle ...
- axios 拦截器统一在接口增加时间戳参数,防止走缓存。
request.interceptors.request.use( config => { if (config.method == 'post') { config.data = { ...c ...
- vue 的全局拦截器
使用拦截器 你可以截取请求或响应在被 then 或者 catch 处理之前 mounted:function(){ Vue.http.inserceptors.push(function(resque ...
随机推荐
- BaseDao优化
BaseDao优化2种方法 1. try { //读取配置文件,配置文件保存了数据库的东西 String path=”database.properties”; //创建实例化对象 Propertie ...
- 深入分析Java I/O的工作机制 (三)网络I/O的工作机制 很详细
3.网络I/O的工作机制 前言:数据从一台主机(服务端)发送到网络中的另一台主机(客户端)需要经过很多步骤:首先需要有相互沟通的意向.其次要有能够沟通的物理渠道(物理链路):是通过电话,还是直接面对面 ...
- RISC-V踩坑记----__builtin_clz((x)库函数的应用
RISC-V的确是个好东西,可是,免费的东西往往需要付出代价才能得到了,最近遇到了一个算法中的问题,追了好久,最终追到了这个库函数中,没想到,这个库函数居然还隐藏着一些猫腻.值得记下来啊. 首先上一个 ...
- Spring Boot Redis Cluster 实战干货
添加配置信息 spring.redis: database: 0 # Redis数据库索引(默认为0) #host: 192.168.1.8 #port: 6379 password: 123456 ...
- odoo开发笔记 -- many2one搜索更多增加默认过滤条件
没加过滤条件的时候,效果如下,点击下拉框,搜索更多出现所有模型下的模板: 改进方法(增加默认过滤条件,显示指定模型下的内容): class IrCloudReport(models.Model): _ ...
- Python xlrd xlwt 读取写入Excel.
import xlrd import xlwt #读取 xlrd.Book.encoding = "gbk" wb = xlrd.open_workbook(filename='s ...
- 容器、容器集群管理平台与 Kubernetes 技术漫谈
原文:https://www.kubernetes.org.cn/4786.html 我们为什么使用容器? 我们为什么使用虚拟机(云主机)? 为什么使用物理机? 这一系列的问题并没有一个统一的标准答案 ...
- linux 命令 — cut
cut 以列的方式格式化输出 依赖定界符 cut -f field_list filename 以默认定界符(tab,制表符)分割文件的列,输出指定的列field_list,field_list由列号 ...
- 逆向实战干货,植物大战僵尸快速定位自动捡阳光Call,或者标志
逆向实战干货,快速定位自动捡阳光Call,或者标志 注意: 关于CE和OD的使用,这里不再多说,快速定位,默认大家已经有了CE基础,或者OD基础. 第一种方法,找Call 第一步,打开CE,搜索阳光值 ...
- 【详解】Tomcat是如何监控并删除超时Session的?
前言 偶然发现Tomcat会话时间的半小时,并不是说会话创建后,只有半小时的有效使用时间,而是说会话空闲半小时后,会被删除.索性就翻了一下源码.做了一番整理. 注:空闲时间,指的是同一个会话两次请求之 ...