Guldasta (A bouquet of flowers)

… because life is about varied experiences

  • Subscribe

  • Blog Stats

    • 37,667 hits
  • Disclaimer

    All which I express here are my independent views/opinions and are not necessarily influenced by any organization I work/correspond with.

Posts Tagged ‘prototype.js ajax request’

Prototype.js lessons

Posted by imdad on September 13, 2008

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
    }
  });
}
Advertisements

Posted in IT | Tagged: | 2 Comments »