Is Safari on iOS 6 caching $.ajax results?

前端 未结 25 1308
轮回少年
轮回少年 2020-11-22 09:34

Since the upgrade to iOS 6, we are seeing Safari\'s web view take the liberty of caching $.ajax calls. This is in the context of a PhoneGap application so it is

25条回答
  •  滥情空心
    2020-11-22 10:03

    I hope this can be of use to other developers banging their head against the wall on this one. I 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

    My solution was the following in my Javascript (all my AJAX requests are POST).

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

    I also add the [pragma: no-cache] header to many of my server responses.

    If you use the above solution be aware that any $.ajax() calls you make that are set to global: false will NOT use the settings specified in $.ajaxSetup(), so you will need to add the headers in again.

提交回复
热议问题