weixin_33725807 2013-03-27 11:09 采纳率: 0%
浏览 24

Extjs4,等待ajax请求

I should run multiple ajax requests in one button click, but all requests should wait until the first one is executed. I have tried to put all requests in the success callback of the first one but this gives this error:

TypeError: o is undefined
return o.id;

And just the first request is executed.
This is my code:

    if(form1.isValid()) {
                form1.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
                    console.log('form1 success');
                    //Submit Form2

                    if(form2.isValid()) {
                        form2.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
                        console.log('form2 success');
}));

//Submit Form3  
.... 

_genFormSubmitAction:

_genFormSubmitAction: function(db,action, successCallback) {
    var me = this;
    return {
        clientValidation : true,
        url              : me.getApplication().apiUrl,
        waitMsg : '<p align=right>..الرجاء الإنتظار</p>',
        async:false,
        params           : {
            _module: 'administrationcassocial',
            _action: action,
            _db:db
        },
        success          : function(form, action) {
            if(action.result.success == true) {
                Ext.callback(successCallback, me);
                form.owner.destroy();
            } else {
                console.log('url=',url);
                Ext.Msg.alert(action.result.error, action.result.errormessages.join("
"));
            }
        },
        failure          : function(form, action) {
            switch (action.failureType) {
                case Ext.form.action.Action.CLIENT_INVALID:
                    Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                    break;
                case Ext.form.action.Action.CONNECT_FAILURE:
                    Ext.Msg.alert('Failure', 'Ajax communication failed');
                    break;
                case Ext.form.action.Action.SERVER_INVALID:
                    Ext.Msg.alert(action.result.error, action.result.errormessages.join("
"));
            }
        }
    };
}
  • 写回答

1条回答 默认 最新

  • H_MZ 2013-03-28 00:27
    关注

    This is a scope issue.

    The callback of form1.submit happens in the callback own scope, so it has no idea what form2 is.

    You can try:

    if(form1.isValid()) {
        var me = this;
        form1.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
            console.log('form1 success');
    
            //Submit Form2
            if( me.form2.isValid() ) {
                form2.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
                    console.log('form2 success');
                }));
            }
        }));
    }
    

    Or the more proper solution in my view:

    // Added aScope var
    _genFormSubmitAction: function( db,action, aScope, successCallback ) {
        var me = this;
        return {    
            // ...
            scope: aScope
        }
    }
    

    Then you call:

    form1.submit(me._genFormSubmitAction('my_DB','my_Action', this, function() {
    }));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘