admin管理员组文章数量:1418041
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 es 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
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 es 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
Share Improve this question edited Oct 26, 2012 at 12:37 Alexis Pigeon 7,51811 gold badges41 silver badges46 bronze badges asked Oct 26, 2012 at 12:11 user1771224user1771224 111 silver badge2 bronze badges 2- That ajax function is asynchronous, so by the time you've returned anything from it, the rest of your function has probably long since been pleted. You need to do the other stuff inside the success (done) handler. – adeneo Commented Oct 26, 2012 at 12:14
- You might want to have a look @ this thread – Jothimurugan B Commented Oct 26, 2012 at 12:36
4 Answers
Reset to default 2error 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'
});
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() });
try throw exception
error: function(){
//...
throw new Exception('Ajax error description');
}
it will stop everything and also you will see it in debug console
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;
}
...
本文标签: javascripthow to exit a function in jquery when an error happens in jqueryajaxStack Overflow
版权声明:本文标题:javascript - how to exit a function in jquery when an error happens in jquery.ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745283521a2651565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论