2012-06-06 3 views
0

Я хочу запомнить ответ запроса ajax, как я могу это сделать? В приведенном выше коде я нашел "" в консоли ... Как это сделать? любые подсказки?Extjs как запомнить ответ от запроса 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); 

ответ

0

Ajax является асинхронной так в то время как вы запустили запрос в вашем Ext.Ajax.request вызова, ответ не вернулся к тому времени console.log (jsoncolumnset) выполняется.

Метод «успех» будет выполняться, когда ответ сервера возвращается в браузер, который может быть миллисекундами или секундами позже - в любом случае код, сопоставленный с событием «успех», выполняется после выполнения команды console.log.

Значит, это фрагмент кода из вложенного в некоторый объект кода, так как у вас есть «эта» область на месте. ,

Вы можете добавить логику, основанную на событиях, которая прекрасно работает с 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 
    } 
}); 
+0

Я еще не пробовал, но я думаю, что он должен работать –

Смежные вопросы