等待从调用函数的jquery ajax回调 [英] wait for a jquery ajax callback from calling function
本文介绍了等待从调用函数的jquery ajax回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经审查了这类问题的很多答案,现在我对最好的方式感到困惑。鉴于最新的jquery,我想要
I have reviewed a lot of answers to this type of question and now I am confused as to the best way. Given the latest jquery, I am wanting to
- 调用ajax函数
- 执行ajax处理成功或错误)//工作正常
- 成功或错误将状态返回到调用函数进行进一步处理
b $ b
在调用函数(doAjax),如何等待回调然后完成成功或错误的处理(在这种情况下,它成功清除表单,错误保持原样)
in the calling function (doAjax), how do I wait for a callback then complete the processing for success or error (in this case its on success clear a form, on error keep it as is)
Thx有任何建议,
Art
有一个错字,你们发现,呼叫应该是doAnAjax不doAjax
Art There was a typo as you guys spotted, call should have been doAnAjax not doAjax
$(function () {
doAnAjax(Url, data, function (myRtn) {
if (myRtn == "success") {
resetForm($('#myForm'));
resetForm($('form[name=addChlGrp]'));
} else {
$('.rtnMsg').html("Opps! Ajax Error");
}
});
});
function doAnAjax(newUrl, data) {
$.ajax({
url: newUrl,
async: true,
dataType: 'html',
beforeSend: function () {
$('.rtnMsg').html("<img src=_cssStyleImg_-A-loading.gif>");
},
type: "GET",
data: data,
cache: false,
success: function (data, textStatus, xhr) {
$('.rtnMsg').html(data);
myRtnA = "Success"
return myRtnA;
},
error: function (xhr, textStatus, errorThrown) {
$('.rtnMsg').html("opps: " + textStatus + " : " + errorThrown);
myRtnA = "Error"
return myRtnA;
}
});
}
推荐答案
一个回调函数。请尝试以下:
You've got to use a callback function. Try this below:
$(function() {
// I think doAjax should doAnAjax()
// here you're passing callback
// but you're not using it doAnAjax()
doAnAjax(Url, data, function(myRtn) {
if (myRtnV == "success") {
resetForm($('#myForm'));
resetForm($('form[name=addChlGrp]'));
} else {
$('.rtnMsg').html("Opps! Ajax Error");
}
});
});
// pass callback as third parameter to doAnAjax()
function doAnAjax(newUrl, data, callBack) {
$.ajax({
url: newUrl,
async: true,
dataType: 'html',
beforeSend: function() {
$('.rtnMsg').html("<img src=_cssStyleImg_-A-loading.gif>");
},
type: "GET",
data: data,
cache: false,
success: function(data, textStatus, xhr) {
$('.rtnMsg').html(data);
myRtnA = "Success"
return callBack( myRtnA ); // return callBack() with myRtna
},
error: function(xhr, textStatus, errorThrown) {
$('.rtnMsg').html("opps: " + textStatus + " : " + errorThrown);
myRtnA = "Error"
return callBack ( myRtnA ); // return callBack() with myRtna
}
});
这篇关于等待从调用函数的jquery ajax回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文