Search code examples
jqueryajaxmobile-safari

How do I not cache AJAX POST in mobile safari app?


Thanks to this post I was able to solve the issue where mobile safari will cache ajax POST requests. Adding "headers: {'Cache-Control': 'no-cache'}" seemed to do the trick for my page in mobile safari.

However, when I access my website via the mobile safari webapp the ajax requests are still cached. I haven't been able to find a solution so thought I'd post here. In addition to adding the header mentioned above, I've also tried adding "cache: false," as well as putting "url: '/ajax_url?v='+time". Nothing seems to work.

Why the different behavior in mobile safari vs. the webapp? How do I resolve this?

EDIT:

Forgot my code. Here it is:

function send_ajax(my_data,refresh){ 
    var now = new Date();
        var n = now.getTime();
    $.ajax({  
      url: "/ajax_page?time=" + n,
      type: "POST",
      headers: {"cache-control": "no-cache"},
      data: my_data,
      dataType: 'json'
      })
      .fail( function (jqXHR, textStatus, errorThrown){
      })
      .done(function(data){ // refresh the page after we get the results
        if(refresh=='true'){
            var pathArray = window.location.pathname.split('/');
            window.location.href = '/' + pathArray[1];

        }
      });         

    }

 send_ajax({'my_checkbox': $('#my_checkbox').is(':checked') },'true');  

Solution

  • One solution that always works is to add a parameter to the request that has the time the request was made (timestamp essentially). This makes every request a unique snowflake that the server has to work on.

    Its similar to the idea of putting the version of your website into the name of all your script/css files so that users that come to your site after an update have to download the new files.