What is the difference between $.ajax();
and $.ajaxSetup();
in jQuery as in:
$.ajax({ cache:false });
and
$.ajaxSetup({ cache:true });
Also, which one is best option?
The ajaxSetup() method in jQuery is used to set the default values for future AJAX requests. Syntax: $.ajaxSetup( {name:value, name:value, ... } )
The cache: false is used by developers to prevent all future AJAX requests from being cached, regardless of which jQuery method they use. We can use $. ajaxSetup({cache:false}); to apply the technique for all AJAX functions.
The following will prevent all future AJAX requests from being cached, regardless of which jQuery method you use ($.get, $.ajax, etc.)
$(document).ready(function() { $.ajaxSetup({ cache: false }); });
you should use $.ajax, which will allow you to turn caching off for that instance:
$.ajax({url: "myurl", success: myCallback, cache: false});
ajaxSetup
sets default values to be valid for all ajax requests. After this you don't have to do the same setting in $.ajax
All settings in $.ajax
will be valid only for that ajax call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With