Mat*_*ada 26 javascript ajax google-chrome
我已经设置了一个应用程序,它在Opera和Firefox上运行得非常棒,但是在谷歌Chrome上,它会缓存AJAX请求并提供过时的数据!
http://gapps.qk.com.au是该应用程序.在Chrome中运行时,它甚至不发送AJAX请求,但是当在另一个浏览器中尝试时,它总是执行AJAX请求并返回数据.
是否有任何方法(Apache/PHP/HTML/JS)阻止Chrome执行此操作?
AJAX电话:
function sendAjax(action,domain,idelement) {
//Create the variables
var xmlhttp,
tmp,
params = "action=" + action
+ "&domain=" + encodeURIComponent(domain)
xmlhttp = new XMLHttpRequest();
//Check to see if AJAX request has been sent
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
$('#'+idelement).html(xmlhttp.responseText);
}
};
xmlhttp.open("GET", "ajax.php?"+params, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//console.log(params);
xmlhttp.send(params);
}
sendAjax('gapps','example.com','gapps');
Run Code Online (Sandbox Code Playgroud)
gau*_*171 32
浏览器缓存在不同设置上的行为有所不同.您不应该依赖用户设置或用户的浏览器.也可以让浏览器忽略标题.
有两种方法可以防止缓存.
- >将AJAX请求更改为POST.浏览器不会缓存POST请求.
- >更好的方式和好方法:使用当前时间戳或任何其他唯一编号为您的请求添加其他参数.
params = "action=" + action
+ "&domain=" + encodeURIComponent(domain)
+ "&preventCache="+new Date();
Run Code Online (Sandbox Code Playgroud)
Mat*_*ada 14
Javascript解决方案的另一个替代方法是使用自定义标头:在PHP中,它看起来像这样:
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");//Dont cache
header("Pragma: no-cache");//Dont cache
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");//Make sure it expired in the past (this can be overkill)
?>
Run Code Online (Sandbox Code Playgroud)