On Aug 20, 11:53 am, ak732 <ask...@gmail.com> wrote:
> Just be aware that doing the ajax call synchronously will block (lock
> up) the browser until the request/response completes.  This is
> typically okay when running locally, but can be very noticeable when
> running on slower connections (e.g. shared hosting).  There's no
> reason why you cannot do this asynchronously.  You just need to handle
> the dom updating inside (or in a sub-function of) the success handler
> function.
>

Ok, I finally got the following function call working from inside the
callback function:

function foobar(jsonData) {
  alert("status = " + jsonData.return_status);
}

function SaveSearch_Success(jsonData) {
   // data = your result in JSON already

   foobar(jsonData);
}

and this was with "async: true" set in the Options object.  One of the
problems I was having yesterday was that I kept getting an "undefined
function" error every time I tried a function call inside the callback
function using jQuery's $.post().  That's why I wanted to stick the
Ajax request JSON return data into a global variable.  I wanted to
write code that would use it outside the callback.  Now the "undefined
function" error seems to have gone away.

I have to write probably at least 100 lines of code to do DOM
updating, and I didn't want to be stuck inside the callback function.
Being able to call multiple functions from within the callback is
exactly the right solution, and I can still keep Ajax requests
asynchronous.


> On Aug 20, 1:13 pm, dkomo872 <dkomo...@comcast.net> wrote:
>
> > On Aug 20, 6:59 am, MorningZ <morni...@gmail.com> wrote:
>
> > > "Does anyone know what is wrong?  I've never seen an ordinary function
> > > behave this way. "
>
> > > That's because $.post (and the resultant $.ajax) isn't "ordinary",
> > > it's an asynchronous call and when you hit the line
>
> > >  jsonData = eval('(' + data + ')');
>
> > > the script isn't back from the post call yet....
>
> > > you need to learn about callbacks to better your code to take full
> > > advantage of async behavior
>
> > > another tip, if you are doing a post and looking to get back JSON,
> > > then use something like:
>
> > > var Options = {
> > >     type = "POST",
> > >     url = "save_search.php",
> > >     processData = true,
> > >     data = formData,
> > >     dataType = "json",
> > >     success = SaveSearch_Success,
> > >     error = SaveSearch_Error};
>
> > > $.ajax(Options);
>
> > > and outside all that, here are your functions "outside" the ajax call
>
> > > function SaveSearch_Success(data) {
> > >    // data = your result in JSON already};
>
> > > function SaveSearch_Error(x,y,z) {
> > >    // x.responseText will have server side error message
>
> > > };
>
> > Ok, I finally got $.ajax(Options) to work, but only after some
> > difficulties.  First of all, your Options above needs to look like
> > this (it's an object):
>
> > var Options = {
> >     type : "POST",
> >     url : "save_search.php",
> >     processData : true,
> >     data : formData,
> >     dataType : "json",
> >     success : SaveSearch_Success,
> >     error : SaveSearch_Error
> >     };
>
> > Secondly, this still produces an asynchronous call to
> > SaveSearch_Success, so I still hit my original probelm with data1
> > being undefined because Javascript rushes ahead without waiting for
> > the Ajax request to complete.  Only when I added the following option
> > to Options:
>
> > async: false
>
> > did I finally get good data in data1 outside of SaveSearch_Success.
> > The above option produces a *synchronous*  Ajax request where
> > Javascript waits until the request is complete.  That's okay with me
> > because the request to save_search.php is very fast anyway.
>
> > Finally, this code snippet results in no arguments being sent to
> > save_search.php:
>
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> > var formData;
>
> > var Options = {
> >     type : "POST",
> >     url : "saved_search.php",
> >     processData : true,
> >     data : formData,
> >     dataType : "json",
> >     success : SaveSearch_Success,
> >     error : SaveSearch_Error,
> >     async: false
> >     };
>
> > formData = $('#form1').serialize();
> > $.ajax(Options);
>
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> > You have to define the Options object *after* putting the arguments
> > into formData like this:
>
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> > var formData = $('#form1').serialize();
>
> > var Options = {
> >     type : "POST",
> >     url : "saved_search.php",
> >     processData : true,
> >     data : formData,
> >     dataType : "json",
> >     success : SaveSearch_Success,
> >     error : SaveSearch_Error,
> >     async: false
> >     };
>
> > $.ajax(Options);
>
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> > Javascript is just *so* cool!  But thanks for your help.  I can
> > continue on and get something productive done now.

Reply via email to