Angular拦截器
来源:互联网 发布:诚哥战斗过的天台淘宝 编辑:程序博客网 时间:2024/05/22 19:10
1
2
3
4
5
6
7
8
9
10
11
module.factory(
'myInterceptor'
, [
'$log'
,
function
($log) {
$log.debug(
'$log is here to show you that this is a regular factory with injection'
);
var
myInterceptor = {
....
....
....
};
return
myInterceptor;
}]);
1
2
3
module.config([
'$httpProvider'
,
function
($httpProvider) {
$httpProvider.interceptors.push(
'myInterceptor'
);
}]);
通过实现 request 方法拦截request: 该方法会在$http发送请求到后台之前执行,因此你可以修改配置或做其他的操作。该方法接收请求配置对象(request configuration object)作为参数,然后必须返回配置对象或者promise 。如果返回无效的配置对象或者 promise 则会被拒绝,导致$http 调用失败。
通过实现response方法拦截response: 该方法会在$http接收到从后台过来的响应之后执行,因此你可以修改响应或做其他操作。该方法接收响应对象(response object)作为参数,然后必须返回响应对象或者promise。响应对象包括了请求配置(request configuration),头(headers),状态(status)和从后台过来的数据(data)。如果返回无效的响应对象或者 promise 会被拒绝,导致$http调用失败。
通过实现requestError方法拦截请求异常: 有时一个请求发送失败或者被拦截器拒绝了。requestError拦截器会捕获那些被上一个请求拦截器中断的请求。它可以用来恢复请求或者有时可以用来撤销请求之前所做的配置,比如关闭遮罩层,显示进度条,激活按钮和输入框之类的。
通过实现responseError方法拦截响应异常: 有时候我们后台调用失败了。也有可能它被一个请求拦截器拒绝了,或者被上一个响应拦截器中断了。在这种情况下,响应异常拦截器可以帮助我们恢复后台调用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module.factory(
'myInterceptor'
, [
'$q'
,
'someAsyncService'
,
function
($q, someAsyncService) {
var
requestInterceptor = {
request:
function
(config) {
var
deferred = $q.defer();
someAsyncService.doAsyncOperation().then(
function
() {
// Asynchronous operation succeeded, modify config accordingly ...
deferred.resolve(config);
},
function
() {
// Asynchronous operation failed, modify config accordingly ...
deferred.resolve(config);
});
return
deferred.promise;
}
};
return
requestInterceptor;
}]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module.factory(
'myInterceptor'
, [
'$q'
,
'someAsyncService'
,
function
($q, someAsyncService) {
var
responseInterceptor = {
response:
function
(response) {
var
deferred = $q.defer();
someAsyncService.doAsyncOperation().then(
function
() {
// Asynchronous operation succeeded, modify response accordingly ...
deferred.resolve(response);
},
function
() {
// Asynchronous operation failed, modify response accordingly ...
deferred.resolve(response);
});
return
deferred.promise;
}
};
return
responseInterceptor;
}]);
Session 注入(request拦截器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module.factory(
'sessionInjector'
, [
'SessionService'
,
function
(SessionService) {
var
sessionInjector = {
request:
function
(config) {
if
(!SessionService.isAnonymus) {
config.headers[
'x-session-token'
] = SessionService.token;
}
return
config;
}
};
return
sessionInjector;
}]);
module.config([
'$httpProvider'
,
function
($httpProvider) {
$httpProvider.interceptors.push(
'sessionInjector'
);
}]);
1
$http.get(
'https://api.github.com/users/naorye/repos'
);
1
2
3
4
5
6
7
8
9
10
11
12
{
"transformRequest"
: [
null
],
"transformResponse"
: [
null
],
"method"
:
"GET"
,
"url"
:
"https://api.github.com/users/naorye/repos"
,
"headers"
: {
"Accept"
:
"application/json, text/plain, */*"
}}
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"transformRequest"
: [
null
],
"transformResponse"
: [
null
],
"method"
:
"GET"
,
"url"
:
"https://api.github.com/users/naorye/repos"
,
"headers"
: {
"Accept"
:
"application/json, text/plain, */*"
,
"x-session-token"
: 415954427904
}}
时间戳(请求和响应拦截器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module.factory(
'timestampMarker'
, [
function
() {
var
timestampMarker = {
request:
function
(config) {
config.requestTimestamp =
new
Date().getTime();
return
config;
},
response:
function
(response) {
response.config.responseTimestamp =
new
Date().getTime();
return
response;
}
};
return
timestampMarker;
}]);
module.config([
'$httpProvider'
,
function
($httpProvider) {
$httpProvider.interceptors.push(
'timestampMarker'
);
}]);
1
2
3
4
$http.get(
'https://api.github.com/users/naorye/repos'
).then(
function
(response) {
var
time = response.config.responseTimestamp - response.config.requestTimestamp;
console.log(
'The request took '
+ (time / 1000) +
' seconds.'
);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module.factory(
'requestRejector'
, [
'$q'
,
function
($q) {
var
requestRejector = {
request:
function
(config) {
return
$q.reject(
'requestRejector'
);
}
};
return
requestRejector;
}]);
module.factory(
'requestRecoverer'
, [
'$q'
,
function
($q) {
var
requestRecoverer = {
requestError:
function
(rejectReason) {
if
(rejectReason ===
'requestRejector'
) {
// Recover the request
return
{
transformRequest: [],
transformResponse: [],
method:
'GET'
,
url:
'https://api.github.com/users/naorye/repos'
,
headers: {
Accept:
'application/json, text/plain, */*'
}
};
}
else
{
return
$q.reject(rejectReason);
}
}
};
return
requestRecoverer;
}]);
module.config([
'$httpProvider'
,
function
($httpProvider) {
$httpProvider.interceptors.push(
'requestRejector'
);
// Removing 'requestRecoverer' will result to failed request
$httpProvider.interceptors.push(
'requestRecoverer'
);
}]);
1
2
3
4
5
$http.get(
'https://api.github.com/users/naorye/repos'
).then(
function
() {
console.log(
'success'
);},
function
(rejectReason) {
console.log(
'failure'
);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module.factory(
'sessionRecoverer'
, [
'$q'
,
'$injector'
,
function
($q, $injector) {
var
sessionRecoverer = {
responseError:
function
(response) {
// Session has expired
if
(response.status == 419){
var
SessionService = $injector.get(
'SessionService'
);
var
$http = $injector.get(
'$http'
);
var
deferred = $q.defer();
// Create a new session (recover the session)
// We use login method that logs the user in using the current credential
//s and returns a promise
SessionService.login().then(deferred.resolve, deferred.reject);
// When the session recovered, make the same backend call again and chain //the request
return
deferred.promise.then(
function
() {
return
$http(response.config);
});
}
return
$q.reject(response);
}
};
return
sessionRecoverer;
}]);
module.config([
'$httpProvider'
,
function
($httpProvider) {
$httpProvider.interceptors.push(
'sessionRecoverer'
);
}]);
阅读全文
0 0
- Angular拦截器
- angular JS 拦截器总结
- Angular $http拦截器介绍与使用
- Angular 拦截器(403、401)
- Angular $http拦截器介绍与使用
- Angular统一拦截器(httpInterceptor)
- Angular $http拦截器计算请求耗时
- Spring mvc拦截器+angular js拦截器 做用户登录拦截控制
- angular 路由拦截
- angular 拦截器每个请求传递用户的token
- 为Angular内置$http服务添加拦截器
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- Angular 4.3 HttpClient (Angular访问 REST Web 服务) 三、拦截器 Interceptors
- angular 未登录状态拦截路由跳转
- angular js 与 springmvc 实现session 超时拦截
- 拦截器
- 拦截器
- 拦截器
- 酷科技 | 智能戒指 ——将科技武装到手指
- JDK 1.8.0_21 bug cpu过高
- VMware 要让“云”隐形
- Hashtable和HashMap
- 生动描述三次tcp握手
- Angular拦截器
- mysql 主从从M-S1-S11 ,S1数据库15张表的数据被删除掉了几千行
- 第十周项目1(2)-二叉树构造算法的验证
- FXML与SceneBuilder开发中TableView控件的使用
- 开源框架MXNet | 环境变量配置(显存)
- 项目部署时,报Jstl异常:org.apache.jasper.JasperException: This absolute uri
- STM32开发板实现Pixhawk无人机GPS跟踪
- xml字符转译
- 机器学习入门:线性回归及梯度下降