Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $.ajax(); and $.ajaxSetup();

Tags:

jquery

ajax

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?

like image 816
Sagar Ranpise Avatar asked Oct 13 '11 06:10

Sagar Ranpise


People also ask

What is ajaxSetup?

The ajaxSetup() method in jQuery is used to set the default values for future AJAX requests. Syntax: $.ajaxSetup( {name:value, name:value, ... } )

What is Ajax cache false?

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.


2 Answers

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}); 
like image 108
Wazy Avatar answered Oct 13 '22 04:10

Wazy


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.

like image 29
Ergec Avatar answered Oct 13 '22 04:10

Ergec