0

Anyone can give me a hint why this getJSON not really calling the PHP page. I am trying to understand some existing code which uses getJSON.

"getfolders.php" page would write a message to a log first as the first step.

My javascript is as below:

$.getJSON('api/getfolders.php', {});
//window.location="api/getfolders.php"; 

If I use getJSON, it is only working sort of first time entering this javascript, if I click CTRL+F5, it doesn't trigger the "getfolders.php" multiple times.

However if comment out getJSON and use window.location instead, every CTRL+F5 will trigger the "getfolders.php" for every time.

Is it some behavior in ajax causing this issue?

Thanks

MLavoie
  • 9,671
  • 41
  • 36
  • 56
yangbin990
  • 83
  • 7
  • Could you add whole script? How is your event listener is registered to specific element? Maybe some dynamically loaded HTML elements don't have assigned event properly – Hubert Sadecki Mar 02 '18 at 13:14

1 Answers1

2

GET requests are cached by the browser. To check it change a request for following 'api/getfolders.php' + Date.now() but it is not good way.

Look at discussion of this subject

Perhaps you should use $ .post () function.

gregman
  • 332
  • 1
  • 11
  • The browser caches the GET requests. You really should use POST to prevent this. Adding a timestamp or a random value which is not cached helps overriding the cache in case of GET (because the URL is different each time), but at the end the browser stores "crap". – Markus Zeller Mar 02 '18 at 16:54