Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent browser from caching AJAX requests

I've setup an app and it works fantastic on Opera and Firefox, but on Google Chrome it caches the AJAX request and will give stale data!

http://gapps.qk.com.au is the app. When ran in Chrome it doesn't even send the AJAX requests, but when tried in another browser it always does the AJAX request and returns data.

Is there any method (Apache/PHP/HTML/JS) to stop Chrome from doing this behavior?

The AJAX call:

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'); 
like image 895
Mattisdada Avatar asked Jul 13 '12 03:07

Mattisdada


People also ask

Do browsers cache AJAX requests?

Fact #1 : Ajax Caching Is The Same As HTTP Caching At this level, the browser doesn't know or care about Ajax requests. It simply obeys the normal HTTP caching rules based on the response headers returned from the server. If you know about HTTP caching already, you can apply that knowledge to Ajax caching.

How do I set cache false in AJAX call?

The cache: false is used by developers to prevent all future AJAX requests from being cached, regardless of which jQuery method they use. We can use $. ajaxSetup({cache:false}); to apply the technique for all AJAX functions.

How do I stop a browser from crashing AJAX requests?

Make sure your ajax requests don't pile up Another problem which might lead to a browser crash is this: in some cases you might use ajax to regularly reload specific parts of your website.

What is ProcessData in AJAX?

ProcessData = true : convert an object's name value pairs into a URL encoding, or an array's objects into name value pairs, or take a string as a literal.


1 Answers

The browser cache behaves differently on different settings. You should not depend on user settings or the user's browser. It's possible to make the browser ignore headers also.

There are two ways to prevent caching.

--> Change AJAX request to POST. Browsers don't cache POST requests.

--> Better Way & good way: add an additional parameter to your request with either the current time stamp or any other unique number.

params = "action=" + action           + "&domain=" + encodeURIComponent(domain)           + "&preventCache="+new Date(); 
like image 178
gaurang171 Avatar answered Oct 05 '22 21:10

gaurang171