通过JQuery ajax提交带有密钥的数组
我通过jquery ajax提交一个值数组,但是当我的servlet有更多的元素时,我的servlet只获取数组中的第一个值。
$.ajax({ type: "POST", url: "myServlet", data: ({'item':itemsArr}) });
数组看起来像: var lovelyArray = ["cake", "thong", "supermanDoll"];
出来的另一面就像: &item=cake
..就是这样。
我希望它能像item=cake&item=thong&item=supermanDoll
在这件事上非常感谢任何帮助。
谢谢。
你可以使用$.param
来序列化你的数组,如下所示:
$.ajax({ type: "POST", url: "myServlet", data: $.param({'item': itemsArr}) // item[]=cake&item[]=thong&item[]=supermanDoll });
以上输出假设您使用的是jQuery 1.4+。 如果您使用的是jQuery 1.3.2或更早版本,输出将如下所示:
item=cake&item=thong&item=supermanDoll
您希望此语法取自http://api.jquery.com/jQuery.post/
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });