The difference between $ .ajax (); and $ .ajaxSetup ();

What is the difference between $.ajax(); and $.ajaxSetup(); in jQuery:

 $.ajax({ cache:false }); 

and

 $.ajaxSetup({ cache:true }); 

Also, which one is best?

+47
jquery ajax
Oct 13 '11 at 6:52
source share
4 answers

Below, caching of all future AJAX requests will be prevented no matter what jQuery method you use ($ .get, $ .ajax, etc.)

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

you should use $ .ajax, which allows you to disable caching for this instance:

 $.ajax({url: "myurl", success: myCallback, cache: false}); 
+97
Oct 13 2018-11-11T00:
source share

ajaxSetup sets default values ​​for all ajax requests. After that you do not need to do the same setup in $.ajax

All settings in $.ajax will be valid only for this ajax call.

+14
Oct 13 2018-11-11T00:
source share

The first one disables the cache for each request, the second sets it for global disabling by default for all AJAX functions.

+5
Oct 13 2018-11-11T00:
source share

To avoid caching, one option is to provide a different URL for the same resource or data. To create a different URL, you can add a random query string to the end of the URL. This method works for jQuery, Angular, or other ajax requests of a different type.

 myURL = myURL +"?random="+new Date().getTime(); 

JQuery uses a similar method with $.ajax({cache:false}); and $.ajaxSetup({cache:false});

$.ajax({cache:false}) applies the method to which it is included, $.ajaxSetup({cache:false}); Applies the technique to all AJAX features.

+2
Apr 22 '16 at 5:35
source share



All Articles