想使用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的更多相关文章

  1. ionic2+Angular 使用HttpInterceptorService拦截器 统一处理数据请求

    sstep1:新建http-Interceptor.ts文件 import { Injectable } from '@angular/core'; import { HttpInterceptorS ...

  2. AngularJS 拦截器实现全局$http请求loading效果

    日常项目开发中,当前端需要和后端进行数据交互时,为了友好的UI效果,一般都会在前端加个loading的状态提示(包括进度条或者icon显示),数据传输或交互完成之后,再隐藏/删除loading提示. ...

  3. axios封装,使用拦截器统一处理接口

    1.项目路径下,引入axios.qs依赖 npm install axios npm install qs 2.在项目的src路径下新建一个commJs文件夹,在commJs文件夹里新建aps.js和 ...

  4. Struts2 在登录拦截器中对ajax请求的处理

    前言: 由于ajax请求不像http请求,可以直接进行页面跳转,你返回的所有东西,ajax都只会识别为一个字符串. 之前尝试的方法是在拦截器中返回一个标识给ajax,然后再在每一个ajax请求成功之后 ...

  5. Okhttp拦截器统一异常处理并多次读取response.body().string()

    参考:https://blog.csdn.net/a624806998/article/details/73863606 引言: 写这篇文章,因为在自己编写实现Http日志拦截器的时候,在拦截器中使用 ...

  6. AngularJS 用 Interceptors 来统一处理 HTTP 请求和响应

    Web 开发中,除了数据操作之外,最频繁的就是发起和处理各种 HTTP 请求了,加上 HTTP 请求又是异步的,如果在每个请求中来单独捕获各种常规错误,处理各类自定义错误,那将会有大量的功能类似的代码 ...

  7. java拦截器与过滤器打印请求url与参数

    HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServle ...

  8. axios 拦截器统一在接口增加时间戳参数,防止走缓存。

    request.interceptors.request.use( config => { if (config.method == 'post') { config.data = { ...c ...

  9. vue 的全局拦截器

    使用拦截器 你可以截取请求或响应在被 then 或者 catch 处理之前 mounted:function(){ Vue.http.inserceptors.push(function(resque ...

随机推荐

  1. 生产apollo搭建记录(五)

    1. 生产apollo搭建记录(五) 1.1. 目标   搭建两个环境配置,dev和pro,但目前可用服务器限制,打算mysql用同一个,服务器分生产和测试 1.2. 数据库 建三个库 注意注意:在启 ...

  2. Python并发目录

    Python并发目录 Python-socket网络编程 Python网络编程-IO阻塞与非阻塞及多路复用 Python进程-理论 Python进程-实现 Python进程间通信 Python进程池 ...

  3. 教你使用docker部署淘宝rap2服务

    什么是rap2 先来说说起因,在上一个星期的分享会上,谈到前后端联调上,有同事提到了rap2,特意去了解了一下,觉得使用这个东西来进行前后端的接口联调来真是太方便了,对比我们之前公司内部开发的API ...

  4. 模板发送java邮件

    Creating email content using a templating library The code in the previous examples explicitly creat ...

  5. Salesforce Sales Cloud 零基础学习(一) Product 和 Price Book

    以前的博客大部分都是基于force.com以及lightning展开的自定义开发,其实salesforce提供了很多的标准的功能以及平台, Sales Cloud便是作为Salesforce核心的平台 ...

  6. leetcode — powx-n

    /** * Source : https://oj.leetcode.com/problems/powx-n/ * * Created by lverpeng on 2017/7/18. * * Im ...

  7. MySQL SQL语句执行顺序

    MySQL的语句一共分为11步,如下图所标注的那样,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来 ...

  8. eclipse导入的项目resource包被当做成文件夹

    项目中遇到的问题: 导出的项目(错误) 原本应该是这样的 需要这样设置一下: 1  2 最后就变回来了!

  9. 【原创】使用golang访问windows telnet服务器

    本篇博客记录本次使用golang语言tcp方式进行telnet服务器访问 环境: 1.win7系统telnet服务器,使用地址:192.168.8.189 2.python使用telnetlib库对t ...

  10. python的partial()用法说明

    在functools模块中有一个工具partial(),可以用来"冻结"一个函数的参数,并返回"冻结"参数后的新函数. 很简单的解释,也是官方手册给的示例.对于 ...