How to prevent caching from jQuery Ajax?

Can someone help me with this? I have a webpage using .manifest to cache offline storage. On this page, I use jQuery ajax to get data from the server. If I load the page first, that's fine. I can switch between online and offline. But the problem is when I return to the Internet and refresh the page. jQuery ajax can no longer talk to the server. Is there a way for ajax to talk to the server or clear the offline cache?

My ajax call is this:

        $.ajax({

            type: "GET",

            url: requestUrl,
            success: localSuccess,

            error: error,

            dataType: "text",
            cache:false
        });
+3
source share
3 answers

I have this code snippet at the top of each page / main page

    //Disable caching in IE, becuase it EVIL!
    $(document).ready(function () {
        $.ajaxSetup({ cache: false });
    });
+5

url "get.php? random =" + Math.random() " . , .

0

, , . . ( , , cache:false, , ). - ajax() . - .

 var callback = function () {
   $.ajax({
      type: "GET",
      url: requestUrl,
      success: localSuccess,
      error: error,
      dataType: "text",

      //won't work sometimes
      cache:false,

      //start random number generator
      data : { r: Math.random() }
     //end random number generator
   });
  }

Thus, every time a server call is made, a new random number is created, and since the data is different from previous requests, ajax requests are not cached and will provide a new server call every time. And on your server, you can ignore this variable r.

0
source

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


All Articles