Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the cache in jQuery $.post() to be false?

Tags:

jquery

I have this code:

$.post("php/tagNotifUnsub.php", $("#tagNotifUnsub").serialize(), function(){ 
            $('#tagSubDiv').load('tags.php #tagSubDiv', function(){$( "button, input:submit, input:button, a#jql, input:radio" ).button();});      
        });

I understand that $.post is just another name for $.ajax. And I know $.ajax has caching on by default. I want it to be uncached. How can I do that?

like image 258
AKor Avatar asked Apr 04 '11 18:04

AKor


People also ask

What is cache false in jQuery?

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.

Why we write cache false in Ajax?

Conclusion. Developer should set cache=false to each ajax request to avoid catastrophic effect in production environment for support to legacy browsers and operating systems.

What is cache false in Ajax call?

cache (default: true, false for dataType 'script' and 'jsonp' ) Type: Boolean. If set to false , it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters.

What is cache true in Ajax?

cache:true is the default and does not always get the content from the cache. The cache-ability of an item on the browser is determined by: The response headers returned from the origin web server. If the headers indicate that content should not be cached then it won't be.


2 Answers

$.post is not cached.

Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

From http://api.jquery.com/jQuery.post/

like image 143
Daniel A. White Avatar answered Sep 22 '22 03:09

Daniel A. White


Partly right, Daniel A. White!

On devices running iOS6 and Safari, POST is also cached.

What you can do is the following:

$.ajaxSetup({
   type: 'POST',
   headers: { "cache-control": "no-cache" }
});
like image 38
Elias Avatar answered Sep 20 '22 03:09

Elias