jQuery anti cache paremeter

jQuery anti cache paremeter

shannonwregeshannonwrege Posts: 22Questions: 9Answers: 0

I have seen it written that to disable the jQuery anti cache parameter, you should set ajax.cache = true. Is this a configuration parameter for the table or something global? Can someone give a code example?

Answers

  • shannonwregeshannonwrege Posts: 22Questions: 9Answers: 0
    edited February 2017

    FYI, I tried setting:

    $.ajaxSetup({ cache: true });

    on the document load but I still get the "_" parameter in the query string of the ajax loads for the datatable.

  • allanallan Posts: 61,642Questions: 1Answers: 10,093 Site admin

    I have seen it written that to disable the jQuery anti cache parameter, you should set ajax.cache = true.

    The exact opposite :smile:. Set it to false to disable - e.g.:

    ajax: {
      url: '...'.
      cache: false
    }
    

    Allan

  • shannonwregeshannonwrege Posts: 22Questions: 9Answers: 0

    Well, I actually want to disable the anti cache parameter (i.e. enable browser caching), but this code snipped is what I needed. Thanks!

  • allanallan Posts: 61,642Questions: 1Answers: 10,093 Site admin

    Yeah the double negative is a bit odd, but cache: false is how you do it in jQuery.

    Allan

  • shannonwregeshannonwrege Posts: 22Questions: 9Answers: 0

    Followup to this earlier question. I have the ajax caching working, but only if I initially load the table that way. In other words:

    var table = $('#my-table').DataTable({
          ajax: {
            url: "/test.json",
            cache: true
          }
    }
    

    and then calling

    table.ajax.url("/test2.json").load(function () {
      ...
    }
    

    works fine. A cached version of test2.json is loaded. It seems that the initial setting instructed future ajax calls to use the cache.

    I can not figure out how to do the initial load from data supplied on the page request and then do a cached ajax call. So if for example I initialize the table this way:

    var table = $('#my-table').DataTable({
          data: variableWithJsonData;
    }
    

    Then executing ...

    table.ajax.url("/test2.json").load(function () {
      ...
    });
    

    ... adds the cache busting querystring parameter. Is there a way to make the ajax load call with instructions to use the cached version if available?

This discussion has been closed.