Sometimes you might need to modify HTTP requests and responses. This could be for a variety of reasons such as adding global logic handling for HTTP errors. With interceptors, you can easily accomplish this in your Angular applications.

var interceptor = function ($q, $location) {
return {
request: function (config) {
console.log(config);
return config;
}, response: function (result) {
console.log('Repos:');
result.data.splice(0, 10).forEach(function (repo) {
console.log(repo.name);
})
return result;
}, responseError: function (rejection) {
console.log('Failed with', rejection.status, 'status');
if (rejection.status == 403) {
$location.url('/login');
} return $q.reject(rejection);
}
}
}; angular.module('app', [])
.config(function ($httpProvider) {
$httpProvider.interceptors.push(interceptor);
})
.run(function ($http) {
$http.get('https://api.github.com/users/bclinkinbeard/reposefw');
});

In a lot of cases, interceptor can be used for Auth.

[AngularJS] Using AngularJS interceptors with $http的更多相关文章

  1. AngularJS 中利用 Interceptors 来统一处理 HTTP 的错误(reproduce)

    原文:http://chensd.com/2016-03/Angular-Handle-Global-Http-Error-with-Interceptors.html?utm_source=tuic ...

  2. 【AngularJS】AngularJS 教程

    AngularJS通过新的属性和表达式扩展了HTML.------------->扩展HTML属性 AngularJS可以构建一个单页面应用程序(SPAs: Single Page Applic ...

  3. AngularJS学习--- AngularJS中模板链接和图像 ng-src step6

    接上一篇文章,本文将主要介绍angularjs中的模板链接,和图像显示? 首先,切换分支,启动项目: git checkout step- npm start 1.效果 相较于前一篇文章,明显感觉多了 ...

  4. AngularJS学习--- AngularJS中XHR(AJAX)和依赖注入(DI) step5

    前言:本文接前一篇文章,主要介绍什么是XHR,AJAX,DI,angularjs中如何使用XHR和DI. 1.切换工具目录 git checkout -f step- #切换分支 npm start ...

  5. AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4

    1.切换工作目录 git checkout step- #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-mo ...

  6. AngularJS学习--- AngularJS中的模板template和迭代器过滤filter step2 step3

    1.AngularJS 模板---step2: mvc(Model-View-Controller)模式在后端用的比较多,在前端也是一样的常用; 在AngularJS中,一个视图是模型通过HTML模板 ...

  7. Angularjs学习---angularjs环境搭建,ubuntu 12.04下安装nodejs、npm和karma

    1.下载angularjs 进入其官网下载:https://angularjs.org/‎,建议下载最新版的:https://ajax.googleapis.com/ajax/libs/angular ...

  8. 夺命雷公狗—angularjs—16—angularjs里面的缓存

    强大的angularjs也给我们预留了一套他的缓存机智,这样在某个程度上来说还是可以做到减轻一点服务器压力的.... <!DOCTYPE html> <html lang=" ...

  9. 夺命雷公狗—angularjs—10—angularjs里面的内置函数

    我们没学一门语言或者框架,几乎里面都有各自的语法和内置函数,当然,强悍的angularjs也不例外,他的方法其实常用的没多少,因为很多都可以用源生jis几乎都能完成一大部分.. <!doctyp ...

随机推荐

  1. Jquery Table 的基本操作

    Jquery 操作 Html Table 是很方便的,这里对表格的基本操作进行一下简单的总结. 首先建立一个通用的表格css 和一个 表格Table: table { border-collapse: ...

  2. Dos操作

    \tree/f >c.txt \dir/s/b >c.txt

  3. 【原创】Linux下使用SecureCRT的方法:破解&编码

    1.下载SecureCRT软件 ubuntu64位:http://download.csdn.net/detail/cobps/7941145 ubuntu32位:http://download.cs ...

  4. xmbc 资源

    http://raspberrypi.diandian.com/post/2013-02-25/40048470423 http://blog.csdn.net/lincyang/article/de ...

  5. 【暑假】[数学]UVa 1262 Password

    UVa 1262  Password 题目: Password   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  6. Windows性能监视器之CPU、硬盘、IO等监控方法详解-摘自网络

    一般操作系统性能主要涉及到的问题主要有:处理器使用情况.内存占有量.磁盘I/0操作以及网络流量等. 查看Windows性能情况,大部分情况下是通过 “Windows任务管理器”,可以通过在 ”命令行” ...

  7. RPi Debian Auto Login

    This guide will show you how to login to a Debian image and start LXDE by simply powering the Pi on. ...

  8. Codeforces Round #138 (Div. 2) ACBDE

    A.Parallelepiped 题意:给一个六面体三面的面积,求12条边的长度和. 题解:因为都是整数,设边长分别为a,b,c,面积为xyz,那么可设x=a*b,y=b*c,z=c*a,简单解方程就 ...

  9. ST-Link STVP Cannot communicate with the device!

    用STLink在ST Visual Programmer中对STM8下载二进制文件有时会出现: 原因:多半是STM8目标板没有电源有问题,或是电源引脚虚焊:

  10. 自定义实现MPVolumeView音量控件

    http://blog.csdn.net/theonezh/article/details/8158420 http://www.cnblogs.com/cate/ios/ http://www.cn ...