IE 對 **GET 請求** 激進快取:同一 URL 參數直接回傳快取,完全不發請求至伺服器。POST 不受影響(參數在 Body)。Firefox/Chrome 預設不快取 AJAX GET。
// jQuery
$.get(url + "?t=" + new Date().getTime(), callback);
// 原生 JS
var url = "/api/data?t=" + Math.random();
$.ajax({
type: "POST", // 或 GET
async: false, // 同步(不建議常用)
cache: false, // ★ 關鍵:自動加入 _=timestamp 參數
url: url,
data: data,
success: function(res) { /* ... */ }
});
cache: false 僅對 GET/HEAD/JSONP 生效;POST 本身不快取。
// Java Servlet
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
// PHP
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
| 方法 | 適用場景 | 優點 | 缺點 |
|---|---|---|---|
| 隨機參數 | 快速修補、舊專案 | 零改動後端 | URL 變長、日誌雜訊 |
| jQuery cache:false | jQuery 專案 | 一行搞定、自動處理 | 僅限 jQuery AJAX |
| 回應標頭 | API 統一治理 | 徹底、所有客戶端受益 | 需後端配合、CDN 可能覆蓋 |
cache: false 或伺服器標頭Cache-Control: no-storeETag/Last-Modified 條件請求