dcp3450
dcp3450

Reputation: 11187

Can I get an array with Jquery $.post?

I using jquery's $.post to pull information from a database dynamically. This works fine if I want one item however, I would like to store each column as an array and pass the whole array over. I have the array but am having issues pulling it back to javascript so I can use it.

Since the array will contain text with commas I can't use implode to create a comma separated string.

Upvotes: 3

Views: 240

Answers (2)

Uku Loskit
Uku Loskit

Reputation: 42040

You must append the "json" like this:

$.post("/your/page", dict,
 function(data) {
  dosomething();

}, "json");

then you can use the .each for looping

jQuery.each(data, function(i, val) {
 alert(i);
 alert(val);

});

or reference values by keys like this:

data['key']

You need to use an associative array. Also, it is required that the data returned by /your/page is JSON encoded (for PHP see json_encode)

Upvotes: 0

Rwky
Rwky

Reputation: 2144

Try using json, if you use php's json_encode on the array on the server side then echo the encoded array jquery will pick it up as a json object.

Upvotes: 1

Related Questions