简体   繁体   English

当jquery.ajax中发生错误时,如何在jquery中退出函数

[英]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. 当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. 例如:如果我正在调用$ .ajax,并且该函数具有$ .ajax之外的其他指令,并且当我的$ .ajax调用出现错误时,如果我尝试调用return以结束其余指令,这些剩余的指令将被执行。

So, what i want is to end the whole function from the erro $.ajax parameter. 因此,我要从erro $ .ajax参数结束整个功能。

$.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 我只是不想,如果$ .ajax错误参数中发生错误以执行其他剩余指令

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: 如果要从containsAjax函数返回某些内容,请使用回调:

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'
});

The jquery ajax method returns a XMLHttpRequest object. jQuery ajax方法返回一个XMLHttpRequest对象。 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() });

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. 您可以在Ajax调用的错误块中编写此语句。

...
 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;
 }
...

try throw exception 尝试抛出异常

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

it will stop everything and also you will see it in debug console 它将停止所有操作,您还将在调试控制台中看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM