Safari Cache Conundrum in iOS 6
Safari's introduction in iOS 6 has raised concerns regarding its cache handling for $.ajax calls. Specifically, POST requests made within a PhoneGap application are being cached despite explicitly setting the cache property to false.
The Root Cause
After thorough investigation, it was discovered that Safari selectively caches POSTs based on the following criteria:
In accordance with the HTTP specification, POST responses are generally not cacheable. However, Safari on iOS 6 appears to be exploiting a provision that allows caching with appropriate Cache-Control or Expires headers.
The Workaround
To prevent Safari from caching POST requests, it is necessary to set "Cache-Control: no-cache" in the response headers. This ensures that Safari honors the cache bypass directive.
For a global solution that applies to all POSTs, add the following code to the server configuration:
<code class="apache">Header set Cache-Control "no-cache"</code>
If you only wish to disable caching for specific POSTs, you can use the following code:
<code class="apache">SetEnvIf Request_Method "POST" IS_POST Header set Cache-Control "no-cache" env=IS_POST</code>
Note: Safari will still cache POST requests if the request parameters remain the same. To avoid caching, modify the request parameters on each call. Alternatively, you can append a random parameter to the request URL or POST data to ensure uniqueness.
The above is the detailed content of Why Does Safari Cache POST Requests in iOS 6 Despite Cache Control Settings?. For more information, please follow other related articles on the PHP Chinese website!