Thursday, November 5, 2009

Re: [jQuery] getJSON - need some direction please.

In AJAX, AJ = Asynchronous JavaScript
Asynchronous means it does not require the browser to freeze while fetching another resource, which is a good thing because the wait may last for several seconds (even more in more extreme cases). The consequence on JavaScript is AJAX methods does not return the result (that's the point really) and continue past the AJAX call as if nothing happened. The AJAX is detached from the earlier script and loads the content in the background, then triggers callback methods when events like success happened.

So, in your exemple :

...
// previous code executes up to the AJAX call

$.getJSON(cdlurl, function(data) {
// this function is a callback called when the AJAX call succeeds, which is whenever appropriate, and carries the current calling context with it which allows you to give it variables it can reuse
var Init_ret = data.company_rte_Initialize.ret;

// you should do here whatever you want with the data you received.

return Init_ret;
// this returns nowhere in particular, returning anything is not the point of this callback
});

// following code continues immediately regardless on what happens for the AJAX call
...

Michel Belleville


2009/11/4 RayJames <cdlcollege.rayjames@gmail.com>
Hi everyone, thanks is advance for your help.

I am working on a getJSON call to my website from a popup window that
is connected through a Run Time Environment API.  I am calling a
function called Initialize().  The call is being made and the data
returned properly.  The crazy thing is that I need to return a simple
"true" or "false" but the crazy thing is that once the call
to .getJSON is made, whatever is executing this code (Browser??) is
not waiting for the function call within the .getJSON to complete
which is where the true/false is returned.  Can anyone point me in the
right direction on what terms to search under or suggest how to
rewrite my code?  Thanks so much.

CODE:
//SORM_API is actaully an object.
function SCORM_API() {

 this.Initialize = function() {
   //return true or false

   //example path would be: cdlonline/node/110/scorm_api/Initialize
   var cdlurl = Drupal.settings.basePath + "node/" +
Drupal.settings.company_sco.nid + "/scorm_api/Initialize";

   //The browser doesn't wait for this .getJSON call to finish, it
moves on without it.  Help.  Thanks.
   $.getJSON(cdlurl, function(data){
       //var below becomes the true/false being returned from the
call to cdlurl.
     var Init_ret = data.company_rte_Initialize.ret; //working.
Becomes true or false.
     return Init_ret;  // where does this return to??
   });
  return /*need to return true or false from above .getJSON call but
browser doesn't wait for the above to finish and  I also don't know
how to get the return value from the Init_ret in the above function.*/
 }
}


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate