How to enable ajax request caching for semi-dynamic data (e.g. JSON)?

I need to cache json data for my phonegap application for 10 minutes, how to do it? The server response already has expiration headers.

Cache-Control: max-age=315360000 Expires: Sun, 12 Sep 2038 20:15:20 GMT 

but jquery ajax request is not cached.

+1
source share
1 answer

[a] Sometimes we need to enable or disable caching of ajax requests for browsers. can be done using the flag under the flag. cache: true

the code:

  global_xhrAbort = $.ajax({ cache: true, type: "GET", timeout: 30000, async: false, url: finalurl, data: null, contentType: "application/x-www-form-urlencoded; charset=UTF-8", dataType: "json", complete: function () { }, success: function (data) { console.log('picked from server koimoi: success'); Page_topstoriesJson = GetJSONifNeeded(data); ; HTMLSTORAGE_SET('landingpage', GetJSONstringfyifNeeded(data)); //will expire in ten mintues doChangesForMainandTopStoriesSlider(); HideLoading(); } , error: function (errmsg) { alert_dialog('Request failed. Please check your internet connection or Press Refresh Button.',true); console.log('index_devicreReadyError: ' + errmsg); } }); 

JQuery AJAX Cupcake Documentation: (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, this will cause the requested pages not to be cached by the browser. Note. Setting the cache to false will only work with HEAD and GET requests. It works by adding "_ = {timestamp}" to the GET parameters. The parameter is not needed for other types of requests, with the exception of IE8, when a POST is created with a URL that has already been requested by GET.

[b] Remember: Ctrl + R on chrome always downloads new data from the server, even if it is cached. Open the page in a new window to see the test results.

0
source

Source: https://habr.com/ru/post/1500761/


All Articles