0

i'm developing a mobile app using cordova . The app consists on sending AJAX requests to a php script on a distant server and then the php extracts the required data from a database and responds with these data in JSON format so the app can parse these data and show them on the app . The problem here is that I have to send the AJAX calls again every time i open the app or browse between the different pages of the app .

here is the function that requests the data :

$.ajax({
                    crossDomain: true,
                    type: "GET",

                    async: false,
                    url: "www.website.com/get-data.php?offset=8&&categorie=all ,
                    dataType: "json",
                    cache: true,

                    success: function (data) {
                     //function to call if success callback
                    }

});

Is there any solution to cache the data or save them some how to prevent unecessary use of the internet connection and the waste of time waiting for the server's response ?

thanks for answering .

ouni hani
  • 21
  • 1
  • 11

3 Answers3

0

You are not exactly describing the kind of data you want to save (is it only strings? is it an array? an object?) or showing any code, so I can only point you to a direction. Take a look at this tutorial for a very quick start and you can learn a few things about sessionStorage and localStorage which might help you with your program. If either of these suggestions fits your needs you can do your own deeper research.

(I am sorry I don't write this in a comment, I just don't have enough reputation)

  • yes the data are in string . i want to cache them without using sessionStorage and localStorage – ouni hani Oct 09 '16 at 20:13
  • In that case I don't have the knowledge to help you. Maybe you can check the solution suggested on this thread: [link](http://stackoverflow.com/questions/17104265/caching-a-jquery-ajax-response-in-javascript-browser) – anima_incognita Oct 09 '16 at 20:19
0

For me i had the same problem and the best way to cache data as a string is localstorage. Then you may also store the last time you updated the data to your localstorage and check data from the server only if the date has updated to a new one.

0

You didn't provide much information about the use of AJAX response in android activities and the period of cache you need.

These are three recommendations for you:

  1. You can use a data change checksum.

    Server should generate a checksum on every request. When you send a request, server would give you the JSON response + current JSON data checksum. You should save (cache) the JSON response + checksum on device (as settings or XML file or JSON file, ...).

    After that every time you need to send a request, you put the last checksum you got, as a HTTP parameter in request. So server checks if the checksum (and so data) isn't changed the server would only return USE_CACHE and the app uses last cached JSON response, otherwise you get new JSON response + new checksum.

    This solution would work on a single request raw. However you can complete it and add supporting for multiple raws (I can help you if you couldn't do..)

  2. Using a closer server
  3. Compressing request and response
ShayanKM
  • 551
  • 1
  • 7
  • 15