简体   繁体   中英

FORCE HttpRequest disable cache without Chrome devtools opened

I have Http request function, that works perfectly on iOS, Android, desktop Opera, but in Chrome it works only with devtools opened. Actually, earlier the same problem was in IE, but I fixed it adding parameters to http request. Currently the function is below:

function requestf(path, run)
{
{
var request = new XMLHttpRequest();
request.open('GET', path+'&cprv='+(new Date().getTime()), true); 
request.setRequestHeader('cache-control', 'no-cache, must-revalidate, post-check=0, pre-check=0');
request.setRequestHeader('expires', 'Thu, 19 Nov 1981 08:52:00 GMT');
request.setRequestHeader('pragma', 'no-cache');

request.addEventListener('readystatechange' ,function()
{
if ((request.readyState==4) && (request.status==200))
run( request.responseText);

}
);

request.send();
}
}

It works in Chrome without devtools is opened when requests are sent not frequently, but in part of code where this function is calling 10 times per a second it works only if devtools is opened. How to fix it?

Literally before pushing the post question button, I wondered, what if to use random number instead of time?

Unfortunately, in my case the issue wasn't in the xhr in general, but what may help you, if you send request with high frequency, as @Pointy mentioned, is getting time value only once, and just increasing it per each request call:

var cache_date = new Date().getTime();

function request(path, callback)
{
cache_date++;

var request = new XMLHttpRequest();
request.open('GET', path+'&cprv='+cache_date, true); 
request.setRequestHeader('cache-control', 'no-cache, must-revalidate, post-check=0, pre-check=0');
request.setRequestHeader('expires', 'Thu, 19 Nov 1981 08:52:00 GMT');
request.setRequestHeader('pragma', 'no-cache');

request.addEventListener('readystatechange' ,function()
{
if ((request.readyState == 4) && (request.status == 200))
callback( request.responseText);
}
);

request.send();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM