使用jquery的ajax来发送请求进行局部刷新画面,各位可能都做过。
今天碰到一个奇怪的现象,就是,同一个ajax请求,在chrome中,不论发送多少次,都可以发送至服务器端,而不会被缓存。但是,换成在IE下的时候,发现,同一个ajax请求,会发生被缓存的情况,只有第一次才会被发送至服务器端,之后的不会再被发送。郁闷。
解决方法如下:
① 直接使用 JQuery提供的 “cache”参数,将其修改为false,即如下(第四行):
$.ajax({
'url' : contextPath + '/file!getAllMajor',
'type' : 'get',
'cache' : false,
'dataType' : 'json',
'async' : true,
'success' : function(data, status, xhr) {
// xxxxx
},
'error' : function(xhr, status, error) {
// xxxxx
}
});
② 这种方案是经常使用的,就是在URL后面添加一个随机数,即如下:
$.ajax({
'url' : contextPath + '/file!getAllMajor?' + Math.random(),
'type' : 'get',
'dataType' : 'json',
'async' : true,
'success' : function(data, status, xhr) {
// xxxxx
},
'error' : function(xhr, status, error) {
// xxxxx
}
});
本文介绍了一个在Internet Explorer浏览器中遇到的Ajax请求被缓存的问题,并提供了两种有效的解决方案:一是通过设置JQuery的cache参数为false;二是通过在URL后附加随机数来避免缓存。

793

被折叠的 条评论
为什么被折叠?



