user1771224
user1771224

Reputation: 11

how to exit a function in jquery when an error happens in jquery.ajax

I'woul like to quit my function when an error happens in jquery ajax. for example: if i'm calling $.ajax with a function which has other instructions out of $.ajax and when it comes that my $.ajax call has an error, if i try to call return to end up the remaining instructions, those remaining instructions are executed.

So, what i want is to end the whole function from the erro $.ajax parameter.

$.ajax({
 type: "POST",
 url: "home.aspx/ReturnInfoAge",
 data: "{'dD':'" + $('#dDate_wx').val() + "','dM':'" + $('#dMonth_wx').val() + "','dY':'" +   $('#dYear_wx').val() + "'}",
 contentType: "application/json; charset=utf-8",
  dataType: "json",
   success: function (msg) {
    if (msg.d === true) {
       prt.children('.tipCont').remove();
     } else {
       getTooltips(prt, 'criticalv', 'critical', msg.d);
        showMessagingTiming('warning', msg.d, 'Vérification de la date de naissance', null, 5000);
         return;
          }
    },
     error: function (errorMsg) {
       getTooltips(prt, 'criticalv', 'critical', "Veuillez bien saisir la date de naissance.");
      showMessagingTiming('warning', 'Veuillez vérifier: certains champs n\'ont pas les valeurs qu\'il faut.', 'Vérification des champs', null, 5000);
       return;
     }
 })
//other instructions 

I just don't want, if an error happens in $.ajax error parameter to execute the other remaining instructions

Upvotes: 1

Views: 5513

Answers (4)

SarveshKaushal
SarveshKaushal

Reputation: 81

If your function is called on some event then you can use the below statement to abort it. You can write this statement in the error block of your Ajax call.

...
 error: function (errorMsg) {
   event.preventDefault();
   getTooltips(prt, 'criticalv', 'critical', "Veuillez bien saisir la date de naissance.");
   showMessagingTiming('warning', 'Veuillez vérifier: certains champs n\'ont pas les valeurs qu\'il faut.', 'Vérification des champs', null, 5000);
   return;
 }
...

Upvotes: 1

sedran
sedran

Reputation: 3566

error and success callbacks are called asynchronously, so your 'other instructions' starts working before one of those callbacks are called. You should write 'other instructions' into success callback.

function containsAjax() {
    $.ajax({
        type: "POST",
        url: "home.aspx/ReturnInfoAge",
        data: "{'dD':'" + $('#dDate_wx').val() + "','dM':'" + $('#dMonth_wx').val() + "','dY':'" +   $('#dYear_wx').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d === true) {
                prt.children('.tipCont').remove();
            } else {
                getTooltips(prt, 'criticalv', 'critical', msg.d);
                showMessagingTiming('warning', msg.d, 'Vérification de la date de naissance', null, 5000);
            }
            //other instructions should be here.
        },
        error: function (errorMsg) {
            getTooltips(prt, 'criticalv', 'critical', "Veuillez bien saisir la date de naissance.");
            showMessagingTiming('warning', 'Veuillez vérifier: certains champs n\'ont pas les valeurs qu\'il faut.', 'Vérification des champs', null, 5000);
        }
    });
}

If you want to return something from containsAjax function, use callbacks:

function containsAjax(onsuccess, onfail) {
    $.ajax({
        type: "POST",
        url: "home.aspx/ReturnInfoAge",
        data: "{'dD':'" + $('#dDate_wx').val() + "','dM':'" + $('#dMonth_wx').val() + "','dY':'" +   $('#dYear_wx').val() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d === true) {
                prt.children('.tipCont').remove();
            } else {
                getTooltips(prt, 'criticalv', 'critical', msg.d);
                showMessagingTiming('warning', msg.d, 'Vérification de la date de naissance', null, 5000);
            }
            //other instructions should be here.
            onsuccess(msg);
        },
        error: function (errorMsg) {
            getTooltips(prt, 'criticalv', 'critical', "Veuillez bien saisir la date de naissance.");
            showMessagingTiming('warning', 'Veuillez vérifier: certains champs n\'ont pas les valeurs qu\'il faut.', 'Vérification des champs', null, 5000);
            onfail(errorMsg);
        }
    });
}

And call it like that:

containsAjax(function(msg) {
    // success callback, returns 'msg'
}, function(errormsg) {
    // error callback, returns 'errormsg'
});

Upvotes: 2

redexp
redexp

Reputation: 5065

try throw exception

error: function(){
    //...
    throw new Exception('Ajax error description');
}

it will stop everything and also you will see it in debug console

Upvotes: 1

defau1t
defau1t

Reputation: 10619

The jquery ajax method returns a XMLHttpRequest object. You can use this object to cancel or abort the request.

var xhr = null;

xhr = $.ajax({
    url : 'path/to/file?some-parameter',
    success : function(responseText) {
        // some DOM manipulation
    }
});

$(document).click(function() { xhr.abort() });

Upvotes: 1

Related Questions