11

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?

AKor
  • 8,550
  • 27
  • 82
  • 136

5 Answers5

32

$.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/

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 4
    Well this is not entirely true, Safari on IOS 6 has cached POST request, it is crazy but it is true. [http://stackoverflow.com/questions/12506897/is-safari-on-ios-6-caching-ajax-results] – Motoo Nov 06 '14 at 08:19
16

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" }
});
Community
  • 1
  • 1
Elias
  • 583
  • 4
  • 12
1

You mention $.ajax

You can turn caching off for ajax like this:

$.ajax({
        url: "/myURL?",
        cache: false,
    });
Stretch
  • 3,669
  • 2
  • 28
  • 40
  • I'm using a GET ajax call and this finally fixed the issue. For some reason some of the requests were being cached and I was getting incorrect data. – AlexL May 08 '17 at 02:42
0

This is an old problem that seems to pop up from time to time and I am always scratching my head. This solution fixes the problem but it may not be suitable in all cases as it is in effect disabling a key feature of AJAX - The asynchronous feature. That said and done for the cases where I have had this problem - Apple products and Safari I'm looking at you especially - setting the additional parameter of async:false in your jQuery AJAX request fixes the issue and actually solves the "No Alert on Post Callback" problem.

As I said if you absolutely rely on asynchronous functionality then this is not going to help you, but for many projects it will stop you pulling your hair out.

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  async:false
});
Athafoud
  • 2,898
  • 3
  • 40
  • 58
0

You need to add NO CACHE to PHP file.

for example

header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32278441) – mcky Jul 22 '22 at 19:58