Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cache false for getJSON in jQuery?

I am using getJSON to fetch the results from server side but facing browser cache problems. I want the cache to be false. I tried using this just before my getJSON call.

 $.ajaxSetup({                 cache: false             }) 

But I am not getting the expected results. It still shows the old results.

I also identified some other solutions such as using .ajax but I really don't want to use that.

like image 915
Abhinav Avatar asked Nov 15 '12 04:11

Abhinav


People also ask

Does getJSON cache?

getJSON(). By adding a unique value to the query string of the request, the request will always be unique and not be cached by the browser - you will always get the latest data. where the number at the end is the timestamp for the moment that the code is called and will therefore always be unique.

What is the difference between getJSON and Ajax in jQuery?

getJSON() is equal to $. ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error. So you were mostly right about the two being pretty much the same :).

What is jQuery getJSON?

jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

What are the arguments of getJSON method?

It is a callback function that executes on the successful server request. It also has three parameters that are data, status, and xhr in which data contains the data returned from the server, status represents the request status like "success", "error", etc., and the xhr contains the XMLHttpRequest object.


1 Answers

Your code just needs a trigger for it to be enabled.

This will allow you to disable cache in all future ajax

$(document).ready(function() {   $.ajaxSetup({ cache: false }); }); 

Hope it helps :)

like image 113
bonesnatch Avatar answered Sep 22 '22 14:09

bonesnatch