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. String中intern的方法

    首先查看官方API那个的解释: ——————————————————————————————————————— intern public String intern() 返回字符串对象的规范化表示形 ...

  2. SQL LEFT JOIN 关键字

    SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行. LEFT JOIN 关键 ...

  3. shell编程——if语句 if -z -n -f -eq -ne -lt

    if  条件then Commandelse Commandfi                              别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...

  4. [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例

    本文我们来学习一下在Entity Framework中使用Context删除多对多关系的实体是如何来实现的.我们将以一个具体的控制台小实例来了解和学习整个实现Entity Framework 多对多关 ...

  5. DELL R710服务器可以安装的VMWare ESX Server 4.1 全套下载带注册码

    随着R710的停产,R720随之面世,但DELL R720服务器只支持vmware esxi5.0以上,DELL客户经理给了一套系统安装后序列号无法解决,还是用4.1好了,却又发现怎么都无法安装.按网 ...

  6. RxJava 复杂场景 Schedulers调度

    参考: https://blog.piasy.com/2016/10/14/Complex-RxJava-2-scheduler/ 以Zip为例,学习Schedulers的线程调度 要求: * cre ...

  7. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

  8. Code Understanding Step by Step - We Need a Task

      Code understanding is a task we are always doing, though we are not even aware that we're doing it ...

  9. Delphi给窗体镶边-为控件加边框,描边,改变边框颜色

    PS:因为我现在用的电脑是WIN7 64位系统,所以没有实现功能,不知道XP是否可行. //1.定义方法 procedure WMNCPaint(var Msg : TWMNCPaint); mess ...

  10. SQL2008-删除时间字段重复的方法

    ID  EMAgitation_ID    YieldDateTime 1   2                              2012-02-11 10:18:54.0002   2  ...