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 ...
随机推荐
- C++ 基础知识回顾总结
一.前言 为啥要写这篇博客?答:之前学习的C和C++相关的知识,早就被自己忘到一边去了.但是,随着音视频的学习的不断深入,和C/C++打交道的次数越来越多,看代码是没问题的,但是真到自己操刀去写一些代 ...
- Java核心技术卷一基础知识-第7章-图形程序设计-读书笔记
第7章 图形程序设计 本章内容: * Swing概述 * 创建框架 * 框架定位 * 在组件中显示信息 * 处理2D图形 * 使用颜色 * 文本使用特殊字体 * 显示图像 本章主要讲述如何编写定义屏幕 ...
- JavaPoet开源项目的使用
一.介绍 JavaPoet项目可以动态的生成Java文件,这是一个很强大和很动态的方法,使用注解的时候假如需要生成新的Java文件就可以通过这个开源项目实现. 项目地址:https://github. ...
- 吴恩达机器学习笔记28-多类分类(Multiclass Classification)
当我们有不止两种分类时(也就是
- Kali学习笔记12:服务扫描
关于什么是服务扫描不多介绍,通俗来看: 我已经扫描到目标机器某个端口开放,接下来我需要知道开放这个端口的是什么应用 情景: 我的Kali机器IP地址:192.168.22.130 我要扫描的Metas ...
- Python档案袋(脚本执行和输入输出)
脚本的执行: 1.执行时确定解释器为Python3 python3 脚本名 2.在代码开始行确定解释器为Python3,与shell相似 env命令表示全局搜索Python3解释器 #!/usr/bi ...
- 使用SAX解析xml文档
1.首先,在main方法中解析xml文档,具体代码如下: import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import ...
- react-native热更新之CodePush详细介绍及使用方法
react-native热更新之CodePush详细介绍及使用方法 2018年03月04日 17:03:21 clf_programing 阅读数:7979 标签: react native热更新co ...
- IdentityServer4(1)- 特性一览
本地应用只本地客户端应用,例如QQ.微信 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架.它可以在您的应用程序中提供以下功能: 它使 ...
- HttpSessionListener的用法
Session创建事件发生在每次一个新的session创建的时候,类似地Session失效事件发生在每次一个Session失效的时候. 这个接口也只包含两个方法,分别对应于Session的创建和失效: ...