2015-12-16 156 views
-1

的Jquery GET禁用缓存Ajax请求

var inputData = { Years: $("#year").val();}; 

    $.get('/MyController/GetData, inputData, function (result) { 
      alert("Success!, I Got My Data"); 
      }); 

如何禁用此缓存?

请帮忙。

回答

1

您只需添加时间戳网址:

$.get('/MyController/GetData?' + $.now(), inputData, function (result) { 
    alert("Success!, I Got My Data"); 
}); 

或者:

var inputData = { _: $.now(), Years: $("#year").val()}; 

$.get('/MyController/GetData', inputData, function (result) { 
    alert("Success!, I Got My Data"); 
}); 
+0

致谢。我会尝试。如果我使用这一行,它会起作用:$ .ajaxSetup({cache:false});? – Billy

+0

它不会,因为你仍然需要它的时间戳。参考:http://api.jquery.com/jquery.ajax/ – Zergling

+0

@Zergling实际上它会工作,因为设置缓存:假附加时间戳作为查询参数 –

1

另一种选择,可以使用$.ajax代替$.get

$.ajax({ 
    url: "/MyController/GetData", 
    data: inputData,  
    cache: false 
}).done(function (result) { 
     alert("Success!, I Got My Data"); 
});