1. 程式人生 > >後臺接收int[] 類型的值

後臺接收int[] 類型的值

scrip style blog 能夠 javascrip mar true 使用 con

1.前臺使用ajax傳遞數組類型的值,後臺無法接收

前臺 js代碼:

 1 $(document).ready(function(){
 2     $("#bu").click(function(){
 3         var ids = new Array(2,3,4);
 4         $.ajax({
 5             url : "${pageContext.request.contextPath}/order/test",
 6             data : {
 7                 "ids" :ids
 8             },
9 dataType : "json", 10 success : function(data) { 11 alert(額); 12 }, 13 error : function() { 14 alert("預覽失敗"); 15 } 16 }); 17 }); 18 });

GET請求參數:

技術分享

後臺 java代碼:

1 @RequestMapping("/test")
2 public
void test(int[] ids,HttpServletRequest request){ 3 String[] id = request.getParameterValues("ids"); 4 }

第一次顯示:

技術分享

id

ids和id 都沒有值;

解決方法:

1.在ajax中加入traditional: true

代碼如下:

 1 $(document).ready(function(){
 2     $("#bu").click(function(){
 3         var ids = new Array(2,3,4);
 4         $.ajax({
5 url : "${pageContext.request.contextPath}/order/test", 6 traditional: true, 7 data : { 8 "ids" :ids 9 }, 10 dataType : "json", 11 success : function(data) { 12 alert(額); 13 }, 14 error : function() { 15 alert("預覽失敗"); 16 } 17 }); 18 }); 19 });

GET請求參數為:

技術分享

後臺

後臺兩種方法都能夠接收到參數:

技術分享

2.把array類型的ids轉為字符串:

 1 $(document).ready(function(){
 2     $("#bu").click(function(){
 3         var ids = new Array(2,3,4);
 4         $.ajax({
 5             url : "${pageContext.request.contextPath}/order/test",
 6             data : {
 7                 "ids" :ids+""
 8             },
 9             dataType : "json",
10             success : function(data) {
11                 alert(額);
12             },
13             error : function() {
14                 alert("預覽失敗");
15             }
16         });
17     });
18 });

GET請求參數:

技術分享

後臺接收到的參數:

技術分享

能夠接收到,但是能轉為int數組,String數組變成一個。

解釋相關:飛機https://my.oschina.net/i33/blog/119506

後臺接收int[] 類型的值