Hey,
I repeated the same mistake .. so i chose to warn you.
Wrong Code:
coolFunction1 = function(){
var data;
new Ajax.Request('some_weird_url_json.php',{
OnSuccess: funtion(transport){
data = transport.responseJSON;
}
});
display(data); // attempting to use the 'data' variable
}
What we wud expect is that the whatever value we have got back through the ajax request will be immediately available, WRONG, callback OnSuccess is only triggered ‘on-success’, so any attempt to use the latest ‘data’ should be inside this callback.
Correct Code:
coolFunction1 = function(){
var data;
new Ajax.Request('some_weird_url_json.php',{
OnSuccess: funtion(transport){
data = transport.responseJSON;
display(data); // phew .. tht was the problem
}
});
}