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 に答える