0

我应该在一个按钮单击中运行多个 ajax 请求,但所有请求都应该等到第一个请求被执行。我试图将所有请求放在第一个的成功回调中,但这会产生此错误:

TypeError: o is undefined
return o.id;

并且只执行第一个请求。
这是我的代码:

    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("\n"));
            }
        },
        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("\n"));
            }
        }
    };
}
4

1 回答 1

0

这是一个范围问题。

form1.submit 的回调发生在回调自己的范围内,所以它不知道是什么form2

你可以试试:

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

或者我认为更合适的解决方案:

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

然后你打电话:

form1.submit(me._genFormSubmitAction('my_DB','my_Action', this, function() {
}));
于 2013-03-28T00:27:23.413 回答