Here's a method that I'm using to pass data from
an ajax response to another function:  (I'm starting
with the success: section of an ajax call)

success:   function(response) {
              if    ( response.MESSAGE == 'Success' )
                    { populateStoryTable(response); }

              else  { alert(Rats!  No good!);       }

That makes all of the data sent back in "response"
available to reference in the function "populateStoryTable".

It's transferred to the populateStoryTable function by using

populateStoryTable(response) {

   ...whatever code I want to run...

That's just a way to directly link the functions with the
data they need.

Michael, this is the method you're referring to which calls
the next function that's needed when the data is ready.

I've used this method of putting variables inside the () after
a function call to pass data all around.

If I do an inline function call, I can use "myFunction(story_id)"
to pass a story_id to the "myFunction" function.

I'm just learning about this stuff, really, so I'm sharing how
I'm managing to make some of the jQuery and especially ajax stuff
work.

I've been working for a month trying to get an ajax app finished
that I could have completed in a day with standard "page-to-page"
processing, passing variables through url's and session, but I'm 
bound and determined to make this work.  I keep writing and re-writing
the app as I learn more.


-----Original Message-----
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Michael Geary
Sent: Thursday, April 09, 2009 8:20 PM
To: jquery-en@googlegroups.com
Subject: [jQuery] Re: Get var out of ajax scope


There's something missing in each of these solutions.

What about the code that will use this variable: How does that code know
when the variable is ready to use?

That code could check to see if the variable is null, but what does it do
then? Try again later? How much later?

What's more likely to be needed is that the complete() function *calls* that
other code as a function. Then you know the data is available, and you can
pass the data value directly to that function as an argument.

You can still put the data in a global variable, but there may not be any
need to do that since you have to make a function call anyway.

-Mike

> -----Original Message-----
> From: jquery-en@googlegroups.com 
> [mailto:jquery...@googlegroups.com] On Behalf Of Eric Garside
> Sent: Thursday, April 09, 2009 4:34 PM
> To: jQuery (English)
> Subject: [jQuery] Re: Get var out of ajax scope
> 
> 
> Well, you've got two basic options. You can do a 
> straightforward global variable like Hector suggested, or you 
> can create and use a custom storage object in the jQuery 
> namespace. Try adding to your
> code:
> 
> $.__customStorage = {};
> 
> $.get({
>   url: 'some.page.php',
>   complete: function(data){
>      $.__customStorage.ajaxResponse = data;
>   }
> });
> 
> On Apr 9, 6:47 pm, Hector Virgen <djvir...@gmail.com> wrote:
> > Something like this might work:
> > var ajaxResponse;
> >
> > $.ajax({
> >     url: 'ajax.php',
> >     complete: function(response) {
> >         ajaxResponse = response;
> >     }
> >
> > });
> >
> > -Hector
> >
> > On Thu, Apr 9, 2009 at 3:44 PM, Nic Hubbard 
> <nnhubb...@gmail.com> wrote:
> >
> > > I have an $.ajax() call that I am using to GET some text 
> from a page 
> > > on my site.  I have put it in a variable with the success 
> function.
> > > How can I move that var up and out of the $.ajax function 
> so I can 
> > > use it in other parts of my script?
> >
> > > Thanks.
> 


Reply via email to