所有从IE发送的ajax调用都被Angular缓存了,我对所有后续调用都得到了一个304响应。虽然请求是一样的,但在我的情况下,响应是不一样的。我想禁用这个缓存。我尝试将缓存属性添加到$http。得到了,但还是没有用。如何解决这个问题?
当前回答
我简单地在angular project的index.html中添加了三个元标签,在IE上就解决了缓存问题。
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">
其他回答
一种选择是使用简单的方法,为每个请求添加时间戳,不需要清除缓存。
let c=new Date().getTime();
$http.get('url?d='+c)
这有点过时了,但是:解决方案已经过时了。让服务器处理缓存或不缓存(在响应中)。保证没有缓存的唯一方法(考虑生产中的新版本)是用版本号更改js或css文件。我用webpack做这个。
我所做的保证是这样的:
myModule.config(['$httpProvider', function($httpProvider) {
if (!$httpProvider.defaults.headers.common) {
$httpProvider.defaults.headers.common = {};
}
$httpProvider.defaults.headers.common["Cache-Control"] = "no-cache";
$httpProvider.defaults.headers.common.Pragma = "no-cache";
$httpProvider.defaults.headers.common["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT";
}]);
为了保证所有方法的正确使用,我不得不合并上述两个解决方案,但你可以用get或其他方法替换common,即put, post, delete,以使其适用于不同的情况。
meta http-equiv="Cache-Control" content="no-cache"
我把这个添加到视图中,它就开始在IE上工作了。确认可以在Angular 2上运行。
试试这个,在类似的情况下它对我很有效:-
$http.get("your api url", {
headers: {
'If-Modified-Since': '0',
"Pragma": "no-cache",
"Expires": -1,
"Cache-Control": "no-cache, no-store, must-revalidate"
}
})