Is Safari on iOS 6 caching $.ajax results?

前端 未结 25 909
轮回少年
轮回少年 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 09:58

    I was able to fix my problem by using a combination of $.ajaxSetup and appending a timestamp to the url of my post (not to the post parameters/body). This based on the recommendations of previous answers

    $(document).ready(function(){
        $.ajaxSetup({ type:'POST', headers: {"cache-control","no-cache"}});
    
        $('#myForm').submit(function() {
            var data = $('#myForm').serialize();
            var now = new Date();
            var n = now.getTime();
            $.ajax({
                type: 'POST',
                url: 'myendpoint.cfc?method=login&time='+n,
                data: data,
                success: function(results){
                    if(results.success) {
                        window.location = 'app.cfm';
                    } else {
                        console.log(results);
                        alert('login failed');
                    }
                }
            });
        });
    });
    
    0 讨论(0)
  • 2020-11-22 09:59

    My workaround in ASP.NET (pagemethods, webservice, etc.)

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
    
    0 讨论(0)
  • 2020-11-22 10:01

    I found one workaround that makes me curious as to why it works. Before reading Tadej's answer concerning ASP.NET web service, I was trying to come up with something that would work.

    And I'm not saying that it's a good solution, but I just wanted to document it here.

    main page: includes a JavaScript function, checkStatus(). The method calls another method which uses a jQuery AJAX call to update the html content. I used setInterval to call checkStatus(). Of course, I ran into the caching problem.

    Solution: use another page to call the update.

    On the main page, I set a boolean variable, runUpdate, and added the following to the body tag:

    <iframe src="helper.html" style="display: none; visibility: hidden;"></iframe>
    

    In the of helper.html:

    <meta http-equiv="refresh" content="5">
    <script type="text/javascript">
        if (parent.runUpdate) { parent.checkStatus(); }
    </script>
    

    So, if checkStatus() is called from the main page, I get the cached content. If I call checkStatus from the child page, I get updated content.

    0 讨论(0)
  • 2020-11-22 10:02

    I just had this issue as well in a PhoneGap application. I solved it by using the JavaScript function getTime() in the following manner:

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

    I wasted a few hours figuring this out. It would have been nice of Apple to notify developers of this caching issue.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 10:03

    This JavaScript snippet works great with jQuery and jQuery Mobile:

    $.ajaxSetup({
        cache: false,
        headers: {
            'Cache-Control': 'no-cache'
        }
    });
    

    Just place it somewhere in your JavaScript code (after jQuery is loaded, and best before you do AJAX requests) and it should help.

    0 讨论(0)
提交回复
热议问题