所有从IE发送的ajax调用都被Angular缓存了,我对所有后续调用都得到了一个304响应。虽然请求是一样的,但在我的情况下,响应是不一样的。我想禁用这个缓存。我尝试将缓存属性添加到$http。得到了,但还是没有用。如何解决这个问题?
当前回答
你可以添加一个拦截器。
myModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('noCacheInterceptor');
}]).factory('noCacheInterceptor', function () {
return {
request: function (config) {
console.log(config.method);
console.log(config.url);
if(config.method=='GET'){
var separator = config.url.indexOf('?') === -1 ? '?' : '&';
config.url = config.url+separator+'noCache=' + new Date().getTime();
}
console.log(config.method);
console.log(config.url);
return config;
}
};
});
您应该在验证后删除console.log行。
其他回答
meta http-equiv="Cache-Control" content="no-cache"
我把这个添加到视图中,它就开始在IE上工作了。确认可以在Angular 2上运行。
我得到它解决附加datetime作为一个随机数:
$http.get("/your_url?rnd="+new Date().getTime()).success(function(data, status, headers, config) {
console.log('your get response is new!!!');
});
为了避免缓存,一种方法是为相同的资源或数据提供不同的URL。为了生成不同的URL,您可以在URL的末尾添加一个随机查询字符串。该技术适用于JQuery、Angular或其他类型的ajax请求。
myURL = myURL +"?random="+new Date().getTime();
这个问题是由于你说的IE缓存问题,你可以在IE调试模式下按f12测试它(这在调试模式下工作很好)。IE不会在每次页面调用时获取服务器数据,而是从缓存中获取数据。要禁用此功能,请执行以下任一操作:
在下面附加HTTP服务请求url
//之前(已发行)
this.httpService.get(this.serviceUrl + “/eAMobileService.svc/ValidateEngagmentName/” + engagementName , {})
//After (working fine)
这httpService得到(this。“服务+”/移动服务。svc/验证名称/ + engagementName + ?日期=“+新日期”。
禁用整个模块的缓存:-
$httpProvider.defaults.headers.common['Pragma'] = '无缓存';
在另一个线程复制我的答案。
对于Angular 2和更新版本,最简单的方法是重写RequestOptions来添加无缓存头文件:
import { Injectable } from '@angular/core';
import { BaseRequestOptions, Headers } from '@angular/http';
@Injectable()
export class CustomRequestOptions extends BaseRequestOptions {
headers = new Headers({
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
});
}
并在你的模块中引用它:
@NgModule({
...
providers: [
...
{ provide: RequestOptions, useClass: CustomRequestOptions }
]
})