Forum Moderators: open

Message Too Old, No Replies

jQuery AJAX Array Response

         

bobbonew

11:18 pm on Jun 4, 2009 (gmt 0)

10+ Year Member



Hey all. I have been able to do fun jquery stuff with the return response, but now on an ajax query I need to get multiple things out of the return.

For instance, if my return is:


Array
(
[body] => Your not really funny.
[guestName] => bobbonew
)

How do I get the information out of there with jquery?

All I can figure out is:


/* .... */

success: function(data){
/*manipulate the return*/
}

And can pretty much do anything with just the return except for putting body and guestName into variables for further manipulation. Any advice?

mehh

9:18 am on Jun 5, 2009 (gmt 0)

10+ Year Member



Global variables are stored as propertys of the window object, so just do something like:
window.ajaxreturn=data

It can then be accessed by just using ajaxreturn as a global, or as a property of window.

bobbonew

9:41 am on Jun 5, 2009 (gmt 0)

10+ Year Member



Well no I know how to access data which is where the return text is stored in. I just wish I could figure out a way to do something like:

alert('Your body is: ' + data.body);

But data just has this in it: (if I alert it it says:)

alert(data);

Array
(
[body] => Your not really funny.
[guestName] => bobbonew
)

whoisgregg

7:55 pm on Jun 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your PHP script, how are you returning the data? Are you using print_r() or are you echo'ing a json_encode() string?

Gibble

9:26 pm on Jun 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how about

alert(data['body']);

EDIT:
or is the response coming back as a string instead of an array?

alert(typeof data);

if it's not an array, you'll need to post the code from the web method you are calling

[edited by: Gibble at 9:38 pm (utc) on June 8, 2009]