1

I'm running into a strange problem on my webpage using the jQuery $.get function. The code below is executed on page load:

$.get(urls.validateSession, function(data){

     console.log(data);

});

I would expect this to make a GET request to urls.validateSession each time the page is loaded, then log the response data to the console.

It does this on the initial page load with no problems. But on subsequent page loads, jQuery does not make a new request to urls.validateSession. It instead just prints the exact same response data from the previous request (identical response timestamp and everything!)

Interestingly, when reloading the page with Shift + F5, jQuery does make a new request to urls.validateSession and I can see a new response timestamp.

Can anyone explain this behavior?

Jaxon Crosmas
  • 397
  • 2
  • 13
  • What have you tried to resolve the problem? Where are you stuck? Is this a PHP problem if the GET request isn't even sent to the server? – Nico Haase Feb 21 '22 at 07:51
  • What have you tried to resolve the problem then? Does your browser's network console show more insights? Is there anything written to your developer console, any error message? – Nico Haase Feb 21 '22 at 08:24

2 Answers2

1

I think this previous question might help get you the desired result. The JQuery get() method's cache is, by default set to true.

cache (default: true, false for dataType 'script' and 'jsonp')

And it sounds like your results are getting cached, that's why it doesn't bother to run the function each page load.

You can check out more ajaxSettings here

The snippet from @KevinB in the linked question:

$.get({
  url: url, 
  cache: false
}).then(function(rdata){
  console.log(rdata);
});
Voorie
  • 61
  • 5
  • Thank you! I guess it would have helped to do a bit of research into the caching for jQuery GET requests. I actually added ```$.ajaxSetup({cache: false,});``` to the beginning of my JavaScript files to disable caching on all ajax requests. – Jaxon Crosmas Feb 21 '22 at 08:53
  • 1
    Awesome! Glad I could help :) Adding `$.ajaxSetup({cache: false,});` is perfect! – Voorie Feb 21 '22 at 09:07
1

If you do not want caching on any of your AJAX requests, you could also set this field in your initial AJAX setup before running any AJAX functions.

$.ajaxSetup({
    cache: false
});