Synchronous Ajax with jQuery

by woody2shoes on June 9, 2009

Earlier, I had a problem getting the result of my Ajax call and operating on it without doing an asynchronous callback. What I wanted to do was assign the result of my ajax call to a variable so I could parse through it and return a result. After a little searching, I found this result.

var result = $.ajax({url: "/my_relative_path", data: {param1: value, param2: value}, async: false}).responseText;

Please note that because responseText returns a string, if your Ajax response includes JSON formatted objects, you need to use eval to convert them to objects.

var result_objects = eval(result);

In my case, I was returning a list of parameter id’s that matched some form fields I need to to change the style of. So, I created a for loop and looped over the objects in my array.

  • Max

    This is just what I needed, thanks!

Previous post:

Next post: