Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate jQuery.ajaxSetup() for each $.ajax()

Tags:

jquery

ajax

How can I set different settings (jQuery.ajaxSetup()) for different AJAX requests?

What I would like to do is to "link" each AJAX call do different ajaxSetup().

like image 212
Eleeist Avatar asked Mar 14 '26 16:03

Eleeist


1 Answers

The point of $.ajaxSetup() is to create default settings for all ajax calls via jQuery on a global scale. If you want to override settings, just specify them in the specific ajax call.

For instance, somewhere early in your code, define your $.ajaxSetup():

$.ajaxSetup({
    type: 'POST'
    , cache: false
    , contentType: 'application/json'
    , dataType: 'json'
    , error: function (a, b, c) {
        //default error handling
        console.log(a, b, c);       
    }
});   

Then when you want to override, say, using a GET, do something like:

$.ajax(myUrl, {
    type: 'GET'
    , data: myData
}); 
like image 162
Jason Avatar answered Mar 17 '26 06:03

Jason