如何将一个Ajax请求中的信息放入另一个Ajax请求中

时间:2016-08-02 09:45:35

标签: javascript php ajax

我有这个代码来获取两部分数据,这两个ajax请求从php类中获取数据。

                       {
                       xtype: 'button',
                       formBind: true,
                       id: 'saveLicenceBtn',
                       text: 'Save',
                       listeners: {
                           click: function (c) {
                               //first ajax request
                               var d = Ext.Ajax.request({
                                   url: 'system/index.php',
                                   params: {
                                       class: 'generatemultiple',
                                       method: 'getSession'
                                   },
                                   success: function (response) {
                                       var object = Ext.decode(response.responseText, true);
                                       console.log(object);
                                   },
                                   failure: function (response) {
                                       var object = Ext.decode(response.responseText, true);
                                       console.log(object);
                                   }
                               });
                               //second ajax request
                               Ext.Ajax.request({
                                   url: 'system/index.php',
                                   method: 'POST',
                                   params: {
                                       class: 'generatemultiple',
                                       method: 'add',
                                       data: Ext.encode({
                                           count: Ext.getCmp('count').getValue(),
                                           start_date: Ext.getCmp('startdateTextField').getValue(),
                                           end_date: Ext.getCmp('enddateTextField').getValue(),
                                           duration: Ext.getCmp('durationTextField').getValue(),
                                           expiry_date: Ext.getCmp('expirydateTextField').getValue(),
                                           product_id: Ext.getCmp('productidTextField').getValue(),
                                           company_id: Ext.getCmp('companyidtf').getValue(),
                                           token: d
                                       })
                                   },
                                   success: function (response) {
                                       Ext.MessageBox.alert('Status', 'Success');
                                       Ext.getStore('LicenseStore').reload();
                                       Ext.getStore('LicenseAllStore').reload();
                                       Ext.getStore('LicenseFeaturesStore').reload();
                                       Ext.getStore('HardwareStore').reload();
                                       Ext.getStore('DropdownLicenseStore').reload();
                                       Ext.getStore('GridHardwareStore').reload();
                                       Ext.getStore('HardwareAllStore').reload();
                                       Ext.getCmp('addLicenseWindow').close();
                                   },
                                   failure: function (response) {
                                       Ext.MessageBox.alert('Status', 'Failure');
                                       Ext.getCmp('addLicenseWindow').close();
                                   }
                               });
                           }
                       }
                   }

第一个ajax请求从网页获取会话变量,第二个ajax请求使用ajax请求发送此标记。我需要知道的是如何在不出现此错误的情况下执行此处显示的内容。

Uncaught RangeError: Maximum call stack size exceeded

我知道这个错误意味着什么,我知道它出现的原因,但我找不到解决方案。它一直在发生,因为我有两个函数相互调用,所以它在Web控制台中出错。

我尝试的是这个

ONCLICK

    click: function (c) {
    Ext.Ajax.request({
        url: 'system/index.php',
        method: 'POST',
        params: {
            class: 'generatemultiple',
            method: 'add',
            data: Ext.encode({
                count: Ext.getCmp('count').getValue(),
                start_date: Ext.getCmp('startdateTextField').getValue(),
                end_date: Ext.getCmp('enddateTextField').getValue(),
                duration: Ext.getCmp('durationTextField').getValue(),
                expiry_date: Ext.getCmp('expirydateTextField').getValue(),
                product_id: Ext.getCmp('productidTextField').getValue(),
                company_id: Ext.getCmp('companyidtf').getValue(),
                token: '<?php echo $_SESSION["user_id"] ?>'
            })
        },
        success: function (response) {
            Ext.MessageBox.alert('Status', 'Success');
            Ext.getStore('LicenseStore').reload();
            Ext.getStore('LicenseAllStore').reload();
            Ext.getStore('LicenseFeaturesStore').reload();
            Ext.getStore('HardwareStore').reload();
            Ext.getStore('DropdownLicenseStore').reload();
            Ext.getStore('GridHardwareStore').reload();
            Ext.getStore('HardwareAllStore').reload();
            Ext.getCmp('addLicenseWindow').close();
        },
        failure: function (response) {
            Ext.MessageBox.alert('Status', 'Failure');
            Ext.getCmp('addLicenseWindow').close();
        }
    });
}

原谅缩进。我想要做的最终目标是使用此ajax请求从php发送会话变量

1 个答案:

答案 0 :(得分:0)

您可以将第一个ajax请求作为同步调用,以便第二个ajax请求将等待第一个ajax请求完成。在第一个请求中设置async: false

相关问题