Is Safari on iOS 6 caching $.ajax results?

前端 未结 25 907
轮回少年
轮回少年 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:54

    Finally, I've a solution to my uploading problem.

    In JavaScript:

    var xhr = new XMLHttpRequest();
    xhr.open("post", 'uploader.php', true);
    xhr.setRequestHeader("pragma", "no-cache");
    

    In PHP:

    header('cache-control: no-cache');
    
    0 讨论(0)
  • 2020-11-22 09:55

    That's the work around for GWT-RPC

    class AuthenticatingRequestBuilder extends RpcRequestBuilder 
    {
           @Override
           protected RequestBuilder doCreate(String serviceEntryPoint) 
           {
                   RequestBuilder requestBuilder = super.doCreate(serviceEntryPoint);           
                   requestBuilder.setHeader("Cache-Control", "no-cache");
    
                   return requestBuilder;
           }
    }
    
    AuthenticatingRequestBuilder builder = new AuthenticatingRequestBuilder();
    ((ServiceDefTarget)myService).setRpcRequestBuilder(builder);    
    
    0 讨论(0)
  • 2020-11-22 09:56

    While adding cache-buster parameters to make the request look different seems like a solid solution, I would advise against it, as it would hurt any application that relies on actual caching taking place. Making the APIs output the correct headers is the best possible solution, even if that's slightly more difficult than adding cache busters to the callers.

    0 讨论(0)
  • 2020-11-22 09:57

    Simple solution for all your web service requests, assuming you're using jQuery:

    $.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()
        }));
    });
    

    Read more about the jQuery prefilter call here.

    If you aren't using jQuery, check the docs for your library of choice. They may have similar functionality.

    0 讨论(0)
  • 2020-11-22 09:57

    From my own blog post iOS 6.0 caching Ajax POST requests:

    How to fix it: There are various methods to prevent caching of requests. The recommended method is adding a no-cache header. This is how it is done.

    jQuery:

    Check for iOS 6.0 and set Ajax header like this:

    $.ajaxSetup({ cache: false });
    

    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) {…}
    

    Server side

    Java:

    httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    

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

    .NET

    Response.Cache.SetNoStore();
    

    Or

    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    

    PHP

    header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
    header('Pragma: no-cache'); // HTTP 1.0.
    
    0 讨论(0)
  • 2020-11-22 09:58

    This is an update of Baz1nga's answer. Since options.data is not an object but a string I just resorted to concatenating the timestamp:

    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
      if (originalOptions.type == "post" || options.type == "post") {
    
        if (options.data && options.data.length)
          options.data += "&";
        else
          options.data = "";
    
        options.data += "timeStamp=" + new Date().getTime();
      }
    });
    
    0 讨论(0)
提交回复
热议问题