Hi Mike,

I think you might have misread my post and the code attached.

I am quite aware of the fact that the callback function only runs upon
a successful completion of the request and is run asynchronously. If
you look at the code i've written you'll see that i am in fact
_counting_ on that since i am declaring variable_from_caller in the
line after the callback code as i know this code will be run before
the callback code (which contains a reference to
variable_from_caller).

My question, again, was in regards to passing data TO the callback
function and not FROM the callback function. In my scenario i have to
consider the possibility of multiple getJSONs called and so multiple
callbacks running side by side so that i cannot use global variables
to store this data, which is specific to each of the callback. The way
i described here uses the actual XMLHttpRequest object (by assigning
$.getJSON to the variable new_json) and only works if i modify the
jQuery code (like i mentioned in my original post).
Is there another way of passing data TO the callback function that is
not global, other than the way i've done it?
Shouldn't getJSON _ALWAYS_ return an XMLHttpRequest object, as the
documentation states?




On Sep 20, 12:17 am, Michael Geary <m...@mg.to> wrote:
> getJSON, like all Ajax and Ajax-style calls, is *asynchronous*. That's what
> the A in Ajax stands for.
>
> Instead of trying to use a return value, you need to use the
> getJSONcallback function (which you're already providing) to do
> whatever you want
> with the data.
>
> So your code might look something like:
>
> $.getJSON( url, function( data ) {
>     // do stuff with data here, such as:
>     callSomeOtherFunction( data );
>
> });
>
> In that callback function, you can store the data anywhere you want. You can
> call other functions. You can do anything you need to.
>
> What you *can't* do is try to use the data immediately after
> getJSONreturns. At that point, the data has not yet been downloaded.
> You have to
> use the callback function and call any other functions you need from there.
>
> -Mike
>
> On Sat, Sep 19, 2009 at 1:16 PM, Blixa <shulgisnotmyem...@gmail.com> wrote:
>
> > I am using getJSON to get results from a different domain and i wanted
> > to get _some_ object back when calling getJSON so that i can insert
> > some variables into the callback scope.
> > Basically, what i wanted to do was this:
>
> > var new_json = $.getJSON(url, function(data) {
> >                                                 alert
> > (this.variable_from_caller);
> >                                                });
> > new_json.variable_from_caller = 'value';
>
> > The problem is that since url is on a different domain, the call to
> > getJSON (which end up calling ajax) returns undefined and not an
> > object that can be referenced (this is on line 3504 of the
> > uncompressed jquery-1.3.2.js).
>
> > I've noticed that if i change undefined to s i can reference the
> > object from outside and have the variable exist within 'this' inside
> > the anonymous callback, although s is not an XMLHttpRequest.
>
> > Is this a bug? am i doing something wrong or twisted and there's a
> > much easier way of accomplishing this in a way i am not aware of?

Reply via email to