js傳遞數組到后台


今天一位同事碰到了這個問題,相互討論了下,記錄下備忘

方法一: 
1.使用JSON.stringify 將數組對象轉化成json字符串;

var array = ["1", "2"]; $.ajax({ type : 'POST', url: path + '/check/testPost', contentType : "application/json" , data : JSON.stringify(array), success : function(data) { } }); 

2.傳輸過程中參數 
這里寫圖片描述

3.后台處理

@RequestMapping(value = "/testPost", method = {RequestMethod.POST}) public void testPost(@RequestBody String[] array) throws IOException { for (String string : array) { System.out.println(string); } return ; }

方法二: 
1.前端不做處理:

var array = ["1", "2"]; $.ajax({ type : 'POST', url: path + '/check/testPost', contentType: "application/x-www-form-urlencoded", data: {"array": array}, success : function(data) { } }); 

2.傳輸過程中參數 
這里寫圖片描述

3.后台處理

@RequestMapping(value = "/testPost", method = {RequestMethod.POST}) public void testPost(HttpServletRequest req) throws IOException { String[] array = req.getParameterValues("array[]"); for (String string : array) { System.out.println(string); } return ; }

注:兩種post請求的content-type不同。

 

 

來源:https://blog.csdn.net/zhaohuijiadelu/article/details/54408324


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM