0

我想记住一个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);
4

1 回答 1

0

Ajax 是异步的,因此当您在 Ext.Ajax.request 调用中启动请求时,在执行 console.log(jsoncolumnset) 时响应还没有返回。

'success' 方法将在服务器响应返回到浏览器时执行,这可能是几毫秒或几秒后 - 无论哪种方式,映射到 'success' 事件的代码都会在 console.log 执行后执行。

因此,由于您拥有“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
    }
});
于 2012-06-07T01:39:06.127 回答