jQuery: The Write Less, Do More JavaScript Library

How can I store data from an AJAX function in a json-array?

View: New views
3 Messages — Rating Filter:   Alert me  

How can I store data from an AJAX function in a json-array?

by DigitalDude :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey,

I need to gather somer data for a jQuery function. I need this data to
be a json array, which I will achieve in a php action which returns an
array that is encoded as JSON.

So far, so good.

I need sth like this:

                                        $.ajax({
                                                        url: '/controller/action',
                                                        data: "ajax=true",
                                                        dataType: 'json',
                                                        complete: function() {
},
                                                        success:
                                                                });
                                                        }
                                        })

but how can I store the data of the returned array into a variable
which I can then use in other functions of my codeblock?

I think this one is easy but I just started learning it and don't know
how to do it :(

Thanks,

DD

Re: How can I store data from an AJAX function in a json-array?

by Michael Geary-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You shouldn't be thinking in terms of "storing the JSON data in a variable for other functions to use."

Instead, you should *call* those other functions from your success callback, and pass the JSON data as an argument to those functions.

Remember that Ajax calls are asynchronous (the A in Ajax). If you store the data in a variable, how will those other functions know when it is ready for use? By calling those functions from the success callback, you insure that the data is ready and available at the moment of the call.

If you have a more complete example I can probably suggest something more specific.

-Mike

On Sat, Nov 7, 2009 at 3:19 PM, DigitalDude <e.blumstengel@...> wrote:
Hey,

I need to gather somer data for a jQuery function. I need this data to
be a json array, which I will achieve in a php action which returns an
array that is encoded as JSON.

So far, so good.

I need sth like this:

                                       $.ajax({
                                                       url: '/controller/action',
                                                       data: "ajax=true",
                                                       dataType: 'json',
                                                       complete: function() {
},
                                                       success:
                                                               });
                                                       }
                                       })

but how can I store the data of the returned array into a variable
which I can then use in other functions of my codeblock?

I think this one is easy but I just started learning it and don't know
how to do it :(

Thanks,

DD


Re: How can I store data from an AJAX function in a json-array?

by DigitalDude :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey,

ok I need to solve this:

      data : function(start, end, callback) {
         callback(getEventData());
      }

Here my data parameter is builded, and the function that is used looks
like this:

function getEventData() {
return {
         events : [
            {
               "id":1,
               "start": new Date(year, month, day, 12),
               "end": new Date(year, month, day, 13, 30),
               "title":"Lunch with Mike"
            }]
}
}

So I need to replace that returning of a directly declared json-array
with an ajax function that gets the data I want to display for me.

I hope there is a solution, it would be very nice because I'm trying
to populate a jQuery calendar with dynamic data...







On 8 Nov., 02:00, Michael Geary <m...@...> wrote:

> You shouldn't be thinking in terms of "storing the JSON data in a variable
> for other functions to use."
>
> Instead, you should *call* those other functions from your success callback,
> and pass the JSON data as an argument to those functions.
>
> Remember that Ajax calls are asynchronous (the A in Ajax). If you store the
> data in a variable, how will those other functions know when it is ready for
> use? By calling those functions from the success callback, you insure that
> the data is ready and available at the moment of the call.
>
> If you have a more complete example I can probably suggest something more
> specific.
>
> -Mike
>
> On Sat, Nov 7, 2009 at 3:19 PM, DigitalDude <e.blumsten...@...>wrote:
>
> > Hey,
>
> > I need to gather somer data for a jQuery function. I need this data to
> > be a json array, which I will achieve in a php action which returns an
> > array that is encoded as JSON.
>
> > So far, so good.
>
> > I need sth like this:
>
> >                                        $.ajax({
> >                                                        url:
> > '/controller/action',
> >                                                        data: "ajax=true",
> >                                                        dataType: 'json',
> >                                                        complete: function()
> > {
> > },
> >                                                        success:
> >                                                                });
> >                                                        }
> >                                        })
>
> > but how can I store the data of the returned array into a variable
> > which I can then use in other functions of my codeblock?
>
> > I think this one is easy but I just started learning it and don't know
> > how to do it :(
>
> > Thanks,
>
> > DD