我想记住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 :(得分:0)
Ajax是异步的,所以当您在Ext.Ajax.request调用中启动请求时,响应时间并没有在console.log(jsoncolumnset)执行时返回。
当服务器响应返回浏览器时,将执行'success'方法,这可能是毫秒或几秒之后 - 无论是在执行console.log之后执行映射到'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
}
});