運用Ajax做了一個名字檢驗,第一次是有效的,但是提交過后,再檢驗一次,結果就不對了,是由于IE的cache的原因。
復制代碼 代碼如下:
function verify() {
$.ajax({
//issue for IE cache; timestamp=" + new Date().getTime()
url:"checkGroupName?timestamp=" + new Date().getTime(),
async: true,
data:"groupName=" + $("#cn").val()+"groupTypeForDetail="+$("#groupType").val()+"prefix="+$("#p").val(),
dataType:"html",
success:function(data){
if(data==1){
$("#result").html("font color='green'>Group name["+$("#p").val()+ $("#cn").val()+"]Valid/font>");
$("#email").val($("#p").val()+ $("#cn").val()+$("#emailHidden").val());
$('#subData').removeAttr("disabled");
}else if(data==2){
$("#result").html("font color='red'>Group name["+$("#p").val()+ $("#cn").val()+ "]already existed./font>");
$('#subData').attr('disabled',"true");
}else{
$("#result").html("font color='red'>Group name can not be empty./font>");
$('#subData').attr('disabled',"true");
}
}
});
}
原理:
Firefox 每次 request 都會重新再回一次 server 取得最新的數據,但是 IE 就不一樣了,它會 cache 住之前得到的數據,只有第一次 request 時會真正的去 server 讀取數據,導致ajax數據不會隨時間而更新….
解決方案(從網上收集的):
1、在服務端加 header("Cache-Control: no-cache, must-revalidate"); 或者用下面的組合更好一些:
復制代碼 代碼如下:
header("Expires: Sat, 1 Jan 2005 00:00:00 GMT");
header("Last-Modified: ".gmdate( "D, d M Y H:i:s")."GMT");
header("cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
2、在ajax發送請求前加上 xmlHttpRequest.setRequestHeader("If-Modified-Since","0");
3、在ajax發送請求前加上 xmlHttpRequest.setRequestHeader("Cache-Control","no-cache");
4、在 Ajax 的 URL 參數后加上 "?fresh=" + Math.random(); //當然這里參數 fresh 可以任意取了
5、第四種方法和第三種類似,在 URL 參數后加上 "?timestamp=" + new Date().getTime(); //推薦使用這種方式,我用的就是這種,個人認為比較方便。
6、用POST替代GET:不推薦
您可能感興趣的文章:- 發布三個ajax相關的函數,包括無刷新提交表單等
- IIS里的AJAX相關的設置
- jquery $.ajax相關用法分享
- ajax上傳時參數提交不更新等相關問題
- js jquery ajax的幾種用法總結(及優缺點介紹)
- 淺談Ajax相關及其優缺點