views:

194

answers:

2

Here is my array:

arr[0]='A';
arr[1]='B';
....

I tried to post it this way:

$.post('data.php',arr,function() {

});

But fails to work as expected.

A: 

You can't post an array. What you need is a hash:

parameters = { "Param1" : "A", "Param2" : "B" };

Choose a suitable name and convert your array to a hash:

hash = {};
$.each(arr, function(i, elem) {
 hash["Param" + i] = elem;
});
kgiannakakis
You can post an array. Read the [answer below](http://stackoverflow.com/questions/2063076/how-to-post-a-array-with-post/2063090#2063090)
Casebash
+2  A: 

From the manual:

data (Optional) Map, String

Key/value pairs or the return value of the .serialize() function that will be sent to the server.

And from the examples in the manual:

$.post("test.php", { 'choices[]': ["Jon", "Susan"] });

Therefore:

$.post("test.php", { 'arr[]': arr });
David Dorward
It is worth noting that JQuery 1.4 adds the [] in [front of your array names automatically](http://benalman.com/news/2009/12/jquery-14-param-demystified/).
Casebash
@Casebash: Eugh.
David Dorward
@David: I know. And worse, it encodes the []
Casebash
Nothing wrong with encoding them.
David Dorward