javascript tutorial - [Solved-5 Solutions] Safari on iOS 6 caching $.ajax - javascript - java script - javascript array



Problem :

Is Safari on iOS 6 caching $.ajax results ?

Solution 1:

developers banging their head against the wall on this one. found that any of the following prevents Safari on iOS 6 from caching the POST response:

  • adding [cache-control: no-cache] in the request headers
  • adding a variable URL parameter such as the current time
  • adding [pragma: no-cache] in the response headers
  • adding [cache-control: no-cache] in the response headers

solution was the following in Javascript

$.ajaxSetup({
    type: 'POST',
    headers: { "cache-control": "no-cache" }
});

click below button to copy the code. By JavaScript tutorial team

Solution 2:

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    // you can use originalOptions.type || options.type to restrict specific type of requests
    options.data = jQuery.param($.extend(originalOptions.data||{}, { 
      timeStamp: new Date().getTime()
    }));
});
click below button to copy the code. By JavaScript tutorial team

Solution 3:

using the JavaScript function getTime() in the following manner:

var currentTime = new Date();
var n = currentTime.getTime();
postUrl = "http://www.wikitechy.com/test.php?nocache="+n;
$.post(postUrl, callbackFunction);

click below button to copy the code. By JavaScript tutorial team

Solution 4:

In JavaScript:

var xhr = new XMLHttpRequest();
xhr.open("post", 'uploader.php', true);
xhr.setRequestHeader("pragma", "no-cache");
click below button to copy the code. By JavaScript tutorial team

In PHP:

header('cache-control: no-cache');
click below button to copy the code. By JavaScript tutorial team

Solution 5:

jQuery: Check for iOS 6.0 and set Ajax header like this:

$.ajaxSetup({ cache: false });
click below button to copy the code. By JavaScript tutorial team

ZeptoJS:

Check for iOS 6.0 and set the Ajax header like this:

$.ajax({
    type: 'POST',
    headers : { "cache-control": "no-cache" },
    url : ,
    data:,
    dataType : 'json',
    success : function(responseText) {…}
click below button to copy the code. By JavaScript tutorial team

Server side

Java:

httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
click below button to copy the code. By JavaScript tutorial team

Make sure to add this at the top the page before any data is sent to the client.

.NET

Response.Cache.SetNoStore();
click below button to copy the code. By JavaScript tutorial team

Or

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
click below button to copy the code. By JavaScript tutorial team

PHP

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Safari on iOS 6 caching $.ajax