2010-12-09 20 views
2

即時通過jquery ajax提交值的數組,但我的servlet只有在數組中有更多元素時纔會選取第一個值。通過JQuery提交一個數組的鍵ajax

$.ajax({ 
     type: "POST", 
     url: "myServlet", 
     data: ({'item':itemsArr})  
}); 

陣列看起來像:var lovelyArray = ["cake", "thong", "supermanDoll"];

出來的另一邊,如:&item=cake ..和多數民衆贊成它。

我期待它出來像item=cake&item=thong&item=supermanDoll

任何幫助,非常感謝在這個問題上。

謝謝。

回答

1

您可以使用$.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 
+0

感謝您的回答花花公子拍攝。無論如何要達到1.3.2與1.4 +相同的產出,即item = cake&item = thong&item = supermanDoll – Julio 2010-12-09 19:29:50