I’m writing a web application and need to initialize some parameters that I’m pulling via the $.getJSON() method.
$.getJSON("../config/", function(data)
{
console.debug(data);
}
Now since these values will be used globally throughout the script and will not be triggering an event directly (which is the only implementation of $.getJSON() I could find in the documentation), how can I returning or retrieve this callback data?
Your best bet is to stick with the callback technique.
There are 2 real ways to make it work, both are essentially the same.
or
Trying to use $.getJSON in a synchronous way ( ie: having it return a value ) will only end in tears and misery for both you and the people using your site, because Synchronous connections have a tendency to block the entire UI. 🙂
As it stands, doing anything like this asynchronously
Will not work, because line 5 is almost guaranteed to execute before line 3.