Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent xmlHttpReq 's browser caching

It seems that all the browsers other than Opera cache XHR requests in my Javascript program. The URL in the XHR request refers to a CGI program on the server which generates a different output everytime its called. B Is there a way in Javascript to make the browser not cache XHR requests?

like image 656
Girish Avatar asked Oct 21 '10 06:10

Girish


People also ask

How do I stop my browser from caching?

When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache. You can then close out of Developer Tools.

Does browser cache automatically?

Browser handles automatically cache of resources. What you seem to be asking about is the actuall response from the server. You will need to set that up yourself in your application. You can do so both on front-end and backend.

Does XMLHttpRequest use cache?

In compatible browsers, if msCaching is not disabled , then XMLHttpRequest instances will be cached to disk regardless of whether cache-control is set to no-cache .

What does browser caching reduce?

Caching minimizes the number of queries that are sent to your server, which takes longer to process than cached results. This is the main reason for caching and how it may improve page performance, load time, and better user experience. While load times are crucial, caching also lowers network costs.


1 Answers

For every AJAX request you make, generate a unique value and add it to the ajax URL as a query parameter:

/example/url/post.php?rand=09809807896768

I use to generate the current unix timestamp in JS and use that - this ensures I do not get duplicated stamps.

That way every request is unique and will not get cached. This is a simple but fairly common AJAX trick - usually fixing IE and testing issues.

If you were to use jQuery, it does this for you by setting the property cache to false on the AJAX settings.

like image 137
Glycerine Avatar answered Oct 20 '22 00:10

Glycerine