I need to assign the ajax response to a global variable so that i can use it thoughout my application. The obvious problem is that ajax requests are async thus complicating things.
I have tried to initialize an object before the ajax call, then assigning the response fron inside the success but that didn't work either.
Any ideas?
Example cod开发者_开发技巧e
var p = {};
loadLang('en');
function loadLang(code) {
Ext.Ajax.request({
url : '/LoadLanguage.html',
method : 'POST',
params :{'code':code},
timeout : 250,
success : function(response, opts) {
obj = Ext.decode(response.responseText);
p = obj;
},
callback : function(o,s,r)
{
}
});
}
var myValue={item:"pie"};
Ext.Ajax.request({
url: 'test.php',
params: {
id: 1
},
success: function(response){
alert(response);
window.myValue.response=response; //yay for global scope.
console.log(window.myValue);//This has to be here,
//else it gets logged before the call gets completed.
}
});
精彩评论