whobutsb
whobutsb

Reputation: 1075

Disabling ajaxSetup Defaults for some AJAX Calls

The corporate intranet I built heavily uses AJAX calls to load tables and other resources. By default I have a modal dialog box appear with a loading gif to let the user know the page is loading. I set this default up with jQuery's ajaxSetup.

There are a couple of areas where I use a search box with autocomplete. And I noticed every time a character is typed the loading dialog box appears. Any way to disable the ajaxSetup for just that call?

Thanks for the help!

Upvotes: 8

Views: 8138

Answers (6)

scottx
scottx

Reputation: 51

We have an $.ajaxSetup like this for every ajax request we run in the app

$.ajaxSetup({
    statusCode: {
        400: function (data) {
            someNiceAlert(data.responseJSON.message);
        }
    }
});

I needed to override this as I was handling errors 400 in a different way for a request and I didn't want the message to appear twice.

My solution was to pass the statusCode as an option into $.ajax like so

$.ajax({
    url: 'myendpoint',
    method: 'put',
    data: editData,
    statusCode: {
        400: function (data) {
            // Override $.ajaxSetup for error 400 - error will be handled below on complete
        },
     }
})...

Upvotes: 0

saif
saif

Reputation: 1214

To avoid some specific URLs I used this way

$.ajaxSetup({
 beforeSend: function (xhr)
            {   
                    if(this.url !="specific urls"){
//code here
                    }
            },
})();

Upvotes: 0

Ravi Patel
Ravi Patel

Reputation: 129

Disabling ajaxSetup Defaults for some AJAX Calls There are one line add

beforeSend: function(){$.event.trigger('ajaxStart');}

beforeSend (Local Event) = This event, will be triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object

Upvotes: 1

Ryan Lynch
Ryan Lynch

Reputation: 7776

I'm guessing that you set up a handler for a global Ajax event to create the dialog box. These events can be disabled for a specific request like so (from the jQuery documentation):

Global Events

These events are broadcast to all elements in the DOM, triggering any handlers which may be listening. You can listen for these events like so:

$("#loading").bind("ajaxSend", function(){
   $(this).show();
 }).bind("ajaxComplete", function(){
   $(this).hide();
 });

Global events can be disabled, for a particular Ajax request, by passing in the global option, like so:

$.ajax({
   url: "test.html",
   global: false,
   // ...
 });

Upvotes: 11

Quintin Robinson
Quintin Robinson

Reputation: 82355

You should be able to pass a new options object with every ajax call to overwrite the settings in the default when needed.

Not sure if your modal is on beforeSend or what have you, but I think you will get the picture from below.

$.ajax({ beforeSend: function() { /* do nothing */ }....

Upvotes: 14

Jake McGraw
Jake McGraw

Reputation: 56106

Use the $.ajax() call to override the defaults established in $.ajaxSetup().

Upvotes: -4

Related Questions