-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-factory-Interceptors.html
70 lines (70 loc) · 3.13 KB
/
angular-factory-Interceptors.html
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>angular-factory-Interceptors(未完)</title>
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="app" ng-controller="MyController">
<script type="text/javascript">
var app = angular.module('app', []);
app.factory('HttpInterceptor', ['$q', '$rootScope', HttpInterceptor]);
function HttpInterceptor($q, $rootScope){
return {
request: function(config){
console.log('request:');
console.log(config);
return config || $q.when(config);
},
requestError: function(err){
console.log('requestError:');
console.log(err);
return err || $q.reject(err);
},
response: function(res){
console.log('response:');
console.log(res);
/*
if (res.config.url == '/api/login') {
// 假设API服务器返回的数据格式如下:
// { token: "AUTH_TOKEN" }
Auth.setToken(res.data.token);
}
*/
return res || $q.when(res);
},
responseError: function(err){
console.log('responseError:');
console.log(err);
switch(rejection.status) {
case 401:
if (rejection.config.url!=='api/login')
// 如果当前不是在登录页面
$rootScope.$broadcast('auth:loginRequired');
break;
case 403:
$rootScope.$broadcast('auth:forbidden');
break;
case 404:
$rootScope.$broadcast('page:notFound');
break;
case 500:
$rootScope.$broadcast('server:error');
break;
}
return $q.reject(err);
}
};
};
app.config(['$httpProvider', function($httpProvider){
console.log($httpProvider);
$httpProvider.interceptors.push(HttpInterceptor);
}]);
app.controller('MyController', ['$scope', '$http', function($scope, $http){
$http.get('https://hq.tigerbrokers.com/fundamental/finance_calendar/getType/2017-02-26/2017-06-10').then(function(data){
console.log(data);
});
}]);
</script>
</body>
</html>