PDA

View Full Version : ajax response to variable



n3b
22 Sep 2010, 6:53 AM
I'm trying to assign the response of an Ext.ajax.request to a variable but I'm failing at every turn. Basically I want to do something like:



function getAjaxJson( url, method ){

var json;
Ext.Ajax.request({
url : url,
method: (method==null) ? 'GET' : method,
timeout: 180000,
success: function ( response ) {
json = Ext.decode(response.responseText);
},
failure: function ( response ) {
Ext.MessageBox.alert('Failed', response.responseText);
},
scope: this
});

return json;
}

var mydata = getAjaxJson("someurl");


I know the scope must be wrong (I see the json variable getting populated then becoming undefined after the callback terminates) - I need some pointers.

All clues welcome.

Cheers,
n

22 Sep 2010, 8:41 AM
your problem is not scope but the fact that you don't know that Ajax requests are asynchronous.

n3b
23 Sep 2010, 3:21 AM
Yeah it was a facepalms moment.

Thanks for the response.

n