Luciano Borges
Luciano Borges

Reputation: 827

Disabling ajaxSetup

I have 3 js files:

app.js

$.ajaxSetup({
    error : function(request) {
        switch (request.status) {
            ...
            case 422:
                App.handle422(request);
                break;
            ...
        }
    }
});

race-registration.js

var RaceRegistrationProxy = {

    url : App.getContextPath() + "/api/event",

    submitRegistration : function(raceId, eventId, data) {
        return $.ajax({
            type : "POST",
            url : this.url + "/" + eventId + "/" + raceId + "/registration",
            data : JSON.stringify(data),
            contentType : "application/json",
            beforeSend : function(request) {
                App.setHeader(request)
            }
        });
    }
}

main.js

...
$("form").submit(function(event) {
    event.preventDefault();
    $("[id$='-message']").hide();
    var data = {
        'team_name' : $("#teamName").val(),
        'category_id' : $("#categoryId").val(),
        'members_ids' : memberIds
    };
    RaceRegistrationProxy.submitRegistration(raceId, eventId, data)
                         .done(registrationOk)
                         .fail(registrationFail);
});
...

How to disable the ajaxSetup when an error occurs in this specific method? I already tried use global : false in the submitRegistration method.

Upvotes: 1

Views: 482

Answers (2)

Maxali
Maxali

Reputation: 1972

You can override it by passing the option each ajax call

All subsequent Ajax calls using any function will use the new settings, unless overridden by the individual calls, until the next invocation of $.ajaxSetup().

like:

return $.ajax({
    type : "POST",
    url : this.url + "/" + eventId + "/" + raceId + "/registration",
    data : JSON.stringify(data),
    contentType : "application/json",
    beforeSend : function(request) {
        App.setHeader(request)
    },
    // here you overrite $.ajaxSetup().
    error : function() {}
});

Upvotes: 1

Luciano Borges
Luciano Borges

Reputation: 827

I add the error atribute to the ajax call in the submitRegistration method.

submitRegistration : function(raceId, eventId, data) {
        return $.ajax({
            type : "POST",
            url : this.url + "/" + eventId + "/" + raceId + "/registration",
            data : JSON.stringify(data),
            contentType : "application/json",
            beforeSend : function(request) {
                App.setHeader(request)
            },
            error : function(){}
        });
    }

Upvotes: 1

Related Questions