我怎样才能强行打开窗户.位置来发出HTTP请求,而不是使用缓存
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。使用true参数重新加载,该参数将始终绕过缓存。
window.location.reload(true);
在url中添加一些特定于时间或随机的查询字符串值。这将强制重新加载页面。
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,只需要相应地转移代码。通过添加新的日期可能会使链接无效,特别是如果你使用临时签名链接等。
欢呼
相关文章:
- Meteor如何接收HTTP请求
- 在我的情况下,如何进行http请求
- 使用插件收听Firefox标签的http请求
- Angularjs http.get() Cross-Origin 请求被阻止
- http请求使用html而不是json进行响应
- 在node-js路由中发出http请求
- 通过sdk/system/events在修改请求观测器上测试http
- 是否可以在没有HTTP请求或<输入>
- Acync JS HTTP请求通知请求
- PhantomJs在控制台中发出http请求时不会返回任何状态
- 当我在chrome中切换设备模式时,$http.get请求失败
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- 使用angular js通过代理服务器请求Http
- 如何从HTTPS服务器请求HTTP服务器的javascript文件
- 阻止的跨来源请求:http://api.fixer.io/latest.
- 坏的请求.HTTP POST到api
- 有没有办法为AngularJS JSON请求($http/$resource)使用浏览器缓存?
- angular js在每个http请求$http上添加请求参数
- 当从不存在的站点请求HTTP响应时,Node.js脚本会中断
- 通过代理服务器请求HTTP呼叫