Extjs如何记忆来自Ajax请求的响应?

x7rlezfr  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(162)

我想记住ajax请求的响应,我该怎么做?在上面的代码中,我在控制台中找到了“”…我该怎么做?有什么建议吗?

var jsoncolumnset = '';

    Ext.Ajax.request({
        scope: this,
        url: 'index.php',
        params: {
            m: 'Columnset',
            a: 'readDefault'
        },
        reader: {
            type: 'json',
            root: 'rows'
        },
        success: function(response){
            //Passo tutto il json (dovrebbe essere fatto un decode, ma viene gestito da Alfresco)     
            jsoncolumnset = response.responseText;
            this.getStore('Documents').proxy.extraParams.columnset = response.responseText;

        },
        failure: function(){
        //TODO: gestione fallimento chiamata
        }
    });
    console.log(jsoncolumnset);
kr98yfug

kr98yfug1#

Ajax是异步的,因此当您在Ext.Ajax中启动请求时。请求调用时,时间控制台尚未返回响应。正在执行日志(jsoncolumnset)。
“success”方法将在服务器响应返回浏览器时执行,这可能需要几毫秒或几秒钟的时间——无论哪种方式,Map到“success'事件的代码都将在控制台之后执行。日志执行。
因此,该代码段似乎来自嵌套在某个对象中的代码,因为您已经设置了“this”范围。
您可以添加一些与ajax配合使用的基于事件的逻辑。这里有一个想法:

// add this custom event in line with other bindings or in the objects constructor or a controllers init method
this.on('columnsready', this.logColumns);

// add this method to the main object
handleColumnResponse: function () {
    //Passo tutto il json (dovrebbe essere fatto un decode, ma viene gestito da Alfresco)     
    this.jsoncolumnset = response.responseText;
    this.getStore('Documents').proxy.extraParams.columnset = response.responseText;

    // fire the custom event
    this.fireEvent('columnsready');

},

// add this as an example of where you would put more logic to do stuff after columns are setup
logColumns: function () {
    console.log(this.jsoncolumnset);
},

Ext.Ajax.request({
    scope: this,
    url: 'index.php',
    params: {
        m: 'Columnset',
        a: 'readDefault'
    },
    reader: {
        type: 'json',
        root: 'rows'
    },

    // map to the handler method in the main object
    success: this.handleColumnResponse,
    failure: function(){
    //TODO: gestione fallimento chiamata
    }
});

相关问题