angular 配置http get 缓存

通常做法就是在http request头部的Cache-Control设置为no-cache,Pragma设置为no-cache,

ie的If-Modified-Since设置为’Mon, 26 Jul 1997 05:00:00 GMT’,

写法如下

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
$httpProvider.interceptors.push(function($q, $location) {
return {
'request':function(req){
if(req.method==='GET'){
console.log(req.method,req.url);
if(!/\.(html)$/.test(req.url)){
if(req.url.substr(req.url.length-1)==='/'){
req.url=req.url+'?t='+new Date().getTime();
}
// else{
// req.url=req.url+'&t='+new Date().getTime();
// }
}
}
return req;
},
'responseError': function(response) {
console.log('response, ' + response.status);
if (response.status === 401 || response.status === 403) {
$location.path('/login');
}
return $q.reject(response);
}
};
});
if(!$httpProvider.defaults.headers.get){
$httpProvider.defaults.headers.get={};
}
//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
// extra
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get.Pragma = 'no-cache';