Home > Web Front-end > JS Tutorial > body text

Why Does iOS 6 Safari Cache $.ajax POST Requests Even When Cache is Set to False?

Susan Sarandon
Release: 2024-11-03 09:23:30
Original
192 people have browsed it

Why Does iOS 6 Safari Cache $.ajax POST Requests Even When Cache is Set to False?

iOS 6 Safari Caching $.ajax POST Requests

Since the release of iOS 6, users have reported an unexpected caching behavior when making $.ajax POST requests within Safari's web view. Despite setting the cache option to false, Safari persistently caches the responses.

Cause

Safari's aggressive caching mechanism specifically targets POST requests that exhibit static function signatures, where the input parameters remain constant and only the returned data varies. By default, Safari considers such requests as cacheable and stores their responses for future retrieval.

Solution

To circumvent this caching issue, modify the function signature to introduce a dynamic parameter. For example, instead of:

getNewRecordID(intRecordType)
Copy after login

Use:

getNewRecordID(intRecordType, strTimestamp)
Copy after login

By incorporating a timestamp or other dynamic parameter into the function signature, Safari recognizes the request as unique and avoids caching the results.

Underlying Reason

Safari's caching behavior stems from an interpretation of the HTTP specification, which allows for caching of POST responses when appropriate Cache-Control or Expires headers are included. However, Apple's implementation extends this caching to scenarios where these headers are absent or set to "max-age=0."

No Cache-Control or Expires headers = iOS6 Safari will cache
Cache-Control max-age=0 and an immediate Expires = iOS6 Safari will cache
Cache-Control: no-cache = iOS6 Safari will NOT cache
Copy after login

Workaround

To disable caching globally for all POST requests, add the following line to the Apache configuration:

Header set Cache-Control "no-cache"
Copy after login

Alternatively, you can limit this behavior specifically to POST requests:

SetEnvIf Request_Method "POST" IS_POST
Header set Cache-Control "no-cache" env=IS_POST
Copy after login

By modifying the function signature and incorporating these caching modifications, you can restore the expected behavior within iOS 6 Safari and ensure that POST requests are not cached.

The above is the detailed content of Why Does iOS 6 Safari Cache $.ajax POST Requests Even When Cache is Set to False?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template