开发者

Submitting an array with a key via JQuery ajax

开发者 https://www.devze.com 2023-01-30 07:16 出处:网络
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.

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"] });
0

精彩评论

暂无评论...
验证码 换一张
取 消