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