![](/img/trans.png)
[英]Is there any way to create a POST HTTP request using window.location?
[英]How can I force window.location to make an HTTP request instead of using the cache?
在我的Web應用程序中,我將window.location
設置為導航到另一個頁面,但是由於某些原因,Firefox顯示了該頁面的舊版本。
使用Firebug,我檢測到瀏覽器甚至沒有發送HTTP請求,它僅使用該頁面的舊版本(甚至沒有最后一個)並顯示它。
頁面本身具有所有常用的標題,以防止緩存,當我使用鏈接或手動輸入瀏覽頁面時,緩存可以完美地起作用。 僅在設置window.location
時出現此問題。
這是Firefox問題還是任何瀏覽器都可以期待的? 這種行為可以改變嗎?
您可以只向頁面URL添加一個隨機參數,以使瀏覽器發出新請求。
所以不要使用
window.location = "my.url/index.html";
采用
window.location = "my.url/index.html?nocache=" + (new Date()).getTime();
您可以使用帶有真實參數的location.reload,它將始終繞過緩存。
window.location.reload(true);
向網址添加一些特定於時間或隨機的查詢字符串值。 這將強制重新加載頁面。
var yourNewLoc = "http://newlocation.com/";
document.location = yourNewLoc + "?c=" + (new Date()).valueOf();
您必須驗證url中是否存在任何現有查詢參數。 如果存在任何查詢參數,則應使用“&”附加時間戳。 我寫了一個簡短的片段,可能對您有所幫助。
window.newLocation = function( location ) {
var newLocation = ( typeof location === "string" ) ? location : window.location.href,
appendType = ( newLocation.indexOf("?") < 0 ) ? "?" : "&";
window.location = newLocation + appendType + "_t=" + (new Date()).getTime();
}
用法:
newLocation("index.html") or window.newLocation("index.html")
// opens "index.html?_t=<timstamp>"
newLocation("index.html?existingQuery=true") or window.newLocation("index.html?existingQuery=true")
// opens "index.html?existingQuery=true&_t=<timstamp
newLocation() or window.newLocation()
// opens existing window location with a timestamp
您可以進一步修改代碼段以刪除查詢參數中的退出時間戳,以避免重復
只需告訴您的下載功能(在控制器中,對於Laravel)就不要通過設置標頭來緩存它,對Laravel使用以下代碼:
$headers =[
'Content-Type' => 'application/text',
'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
'Cache-Control' => 'post-check=0, pre-check=0, false',
'Pragma' => 'no-cache', ];
return response()->file($pathToFile, $headers);
對於PhP來說,此代碼非常正確,因此只需相應地傳輸代碼即可。 通過添加新日期可能會使鏈接無效,尤其是在您使用temporarySignedLink等的情況下。
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.