HighDots Forums  

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

jQuery jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript.


Discuss How can I store data from an AJAX function in a json-array? in the jQuery forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
DigitalDude
 
Posts: n/a

Default How can I store data from an AJAX function in a json-array? - 11-07-2009 , 06:19 PM






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

Reply With Quote
  #2  
Old   
Michael Geary
 
Posts: n/a

Default Re: [jQuery] How can I store data from an AJAX function in ajson-array? - 11-07-2009 , 08:00 PM






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 (AT) googlemail (DOT) com>wrote:

Quote:
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

Reply With Quote
  #3  
Old   
DigitalDude
 
Posts: n/a

Default Re: How can I store data from an AJAX function in ajson-array? - 11-07-2009 , 08:39 PM



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... (AT) mg (DOT) to> wrote:
Quote:
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... (AT) googlemail (DOT) com>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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.