Submitting an array with a key via JQuery ajax
im submitting an array of values via jquery ajax, but my servlet only picks up the first value in the array, when it has many more elements.
$.ajax({
type: "POST",
url: "myServlet",
data: ({'item':itemsArr})
});
the array looks something like: var lovelyArray = ["cake", "thong", "supermanDoll"];
comes out the other side like: &item=cake
.. and thats it.
i'm expecting it to 开发者_高级运维come out like item=cake&item=thong&item=supermanDoll
Any help is much appreciated on this matter.
Thanks.
You can use $.param
to serialize your array, like this:
$.ajax({
type: "POST",
url: "myServlet",
data: $.param({'item': itemsArr}) // item[]=cake&item[]=thong&item[]=supermanDoll
});
The above output assumes that you are using jQuery 1.4+. If you are using jQuery 1.3.2 or earlier, the output will look like:
item=cake&item=thong&item=supermanDoll
You want this syntax instead taken from http://api.jquery.com/jQuery.post/
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
精彩评论