等待调用函数一个jQuery Ajax回调 [英] wait for a jquery ajax callback from calling function

查看:242
本文介绍了等待调用函数一个jQuery Ajax回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查阅了很多答案这种类型的问题,我现在很困惑,最好的方式。鉴于最新的jQuery,我想

  1. 在调用一个Ajax功能
  2. 请阿贾克斯处理(成功或错误)//正常工作
  3. 在成功或错误状态返回到调用函数进行进一步处理

在调用函数(doAjax),我怎么等待回调再完成处理成功或错误(在这种情况下,其成功清除表单上的错误保持原样)

THX任何意见,

艺术 有一个错字为你们发现,调用应该是doAnAjax没有doAjax

  $(函数(){
    doAnAjax(地址,数据,功能(myRtn){
        如果(myRtn ==成功){
            resetForm($('#myForm会'));
            resetForm($('形式[名称= addChlGrp]'));
        } 其他 {
            $('rtnMsg。)HTML(哎呀阿贾克斯错误!)。
        }
    });
});

功能doAnAjax(的newURL,数据){
    $阿贾克斯({
        网址:的newURL,
        异步:真正的,
        数据类型:HTML,
        beforeSend:函数(){
            $('rtnMsg。)HTML(< IMG SRC = _cssStyleImg_-A-loading.gif>中)。
        },
        键入:GET,
        数据:数据,
        缓存:假的,
        成功:功能(数据,textStatus,XHR){
            $('。rtnMsg')的HTML(数据)。
            myRtnA =成功
            返回myRtnA;
        },
        错误:函数(XHR,textStatus,errorThrown){
            $('。rtnMsg')的HTML(OPPS:+ textStatus +:+ errorThrown);
            myRtnA =错误
            返回myRtnA;
        }
    });
}
 

解决方案

您已经使用回调函数。尝试像如下:

  $(函数(){

   //我觉得doAjax应该doAnAjax()
   //这里你传递回调
   //但你不使用它doAnAjax()

    doAnAjax(地址,数据,功能(myRtn){
        如果(myRtnV ==成功){
            resetForm($('#myForm会'));
            resetForm($('形式[名称= addChlGrp]'));
        } 其他 {
            $('rtnMsg。)HTML(哎呀阿贾克斯错误!)。
        }
    });
});

//通过回调作为第三个参数doAnAjax()

功能doAnAjax(的newURL,数据,回调){
    $阿贾克斯({
        网址:的newURL,
        异步:真正的,
        数据类型:HTML,
        beforeSend:函数(){
            $('rtnMsg。)HTML(< IMG SRC = _cssStyleImg_-A-loading.gif>中)。
        },
        键入:GET,
        数据:数据,
        缓存:假的,
        成功:功能(数据,textStatus,XHR){
            $('。rtnMsg')的HTML(数据)。
            myRtnA =成功
            返回回调(myRtnA); //返回回调()与myRtna
        },
        错误:函数(XHR,textStatus,errorThrown){
            $('。rtnMsg')的HTML(OPPS:+ textStatus +:+ errorThrown);
            myRtnA =错误
            返回回调(myRtnA); //返回回调()与myRtna
        }
    });
 

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

  1. Call an ajax function
  2. do ajax processing (success or error) // works fine
  3. on success or error return the status to the calling function for further processing

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 for any advice,

Art [EDIT] 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 to use callback function. Try like 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆