zoukankan      html  css  js  c++  java
  • ajax传递list集合

    原文链接:https://blog.csdn.net/qq_37936542/article/details/79277495

    一:ajax传递List<String>类型的数据

    js代码:

    1. //声明list  
    2. var _list = [];  
    3. //放入string对象  
    4. for (var i = 0; i < 3; i++) {  
    5.     _list[i]="tom";  
    6. }  
    7.   
    8. $.ajax({  
    9.     url : '/ajax/test',  
    10.     data : "list="+_list,  
    11.     type : "POST",  
    12.     success : function(data) {  
    13.         alert(data);  
    14.     }  
    15. });  

    java代码:
    1. @RequestMapping(value="test",method=RequestMethod.POST)  
    2. @ResponseBody  
    3. public String ajaxList(@RequestParam("list")List<String> strList){  
    4.           
    5.     for (String str : strList) {  
    6.         System.out.println(str);  
    7.     }  
    8.     return "OK";  
    9. }  


    二:ajax传递List<Obj>类型的数据

    后台需要用到json解析工具,我选得是jackson

    导入jackson依赖:

    1. <dependency>  
    2.     <groupId>com.fasterxml.jackson.core</groupId>  
    3.     <artifactId>jackson-databind</artifactId>  
    4.     <version>2.7.3</version>  
    5. </dependency>  

    js代码:
    1. //声明list  
    2. var _list = [];  
    3. //创建两个user对象  
    4. var a= {};  
    5. a.name="tom";  
    6. a.age=23;  
    7. a.city="上海";  
    8. var b = {};  
    9. b.name="jack";  
    10. b.age=25;  
    11. a.city="安徽";  
    12. //将user放入_list  
    13. _list.push(a);  
    14. _list.push(b);  
    15.   
    16. $.ajax({  
    17.     url : '/ajax/test1',  
    18.     data : "list="+JSON.stringify(_list),  
    19.     type : "POST",  
    20.     success : function(data) {  
    21.         alert(data);  
    22.     }  
    23. });  


    java代码:
    1. @RequestMapping(value="test",method=RequestMethod.POST)  
    2. @ResponseBody  
    3. public String ajaxList(@RequestParam("list")String userList) throws Exception{  
    4.     //jackson对象  
    5.     ObjectMapper mapper = new ObjectMapper();  
    6.     //使用jackson将json转为List<User>  
    7.     JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);     
    8.     List<User> list =  (List<User>)mapper.readValue(userList, jt);  
    9.           
    10.     return "OK";  
    11. }  


    三:当ajax传递任何复杂参数时,后台可以直接从流中来读取数据进行解析

    js代码:

    1. //声明list  
    2. var _list = [];  
    3. //创建两个user对象  
    4. var a= {};  
    5. a.name="tom";  
    6. a.age=23;  
    7. a.city="上海";  
    8. var b = {};  
    9. b.name="jack";  
    10. b.age=25;  
    11. a.city="安徽";  
    12. //将user放入_list  
    13. _list.push(a);  
    14. _list.push(b);  
    15.   
    16. $.ajax({  
    17.     url : '/querz/test',  
    18.     data : JSON.stringify(_list),//这里需要json化  
    19.     type : "POST",  
    20.     success : function(data) {  
    21.         alert(data);  
    22.     }  
    23. });  

    java代码:
    1. @RequestMapping(value="test",method=RequestMethod.POST)  
    2. @ResponseBody  
    3. public String ajaxList(HttpServletRequest request) throws Exception{  
    4.       
    5.     //从流中读取数据  
    6.     BufferedReader br = request.getReader();  
    7.     String str = "";  
    8.     StringBuffer sb = new StringBuffer();  
    9.     while((str = br.readLine()) != null){  
    10.         sb.append(str);  
    11.     }  
    12.           
    13.     ObjectMapper mapper = new ObjectMapper();  
    14.     //使用jackson解析数据  
    15.     JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);     
    16.     List<User> list =  (List<User>)mapper.readValue(sb.toString(), jt);   
    17.     System.out.println(list);  
    18.           
    19.     return "OK";  

    文末福利:

    福利一:前端,Java,产品经理,微信小程序,Python等10G资源合集大放送:https://www.jianshu.com/p/e8197d4d9880

    福利二:微信小程序入门与实战全套详细视频教程

    【领取方法】

    关注 【编程微刊】微信公众号:

    回复【小程序demo】一键领取130个微信小程序源码demo资源。

    回复【领取资源】一键领取前端,Java,产品经理,微信小程序,Python等资源合集10G资源大放送。


    image

    90后前端妹子,爱编程,爱运营,爱折腾。
    坚持总结工作中遇到的技术问题,坚持记录工作中所所思所见,欢迎大家一起探讨交流。


  • 相关阅读:
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    449. Serialize and Deserialize BST
    114. Flatten Binary Tree to Linked List
    199. Binary Tree Right Side View
    173. Binary Search Tree Iterator
    98. Validate Binary Search Tree
    965. Univalued Binary Tree
    589. N-ary Tree Preorder Traversal
    eclipse设置总结
  • 原文地址:https://www.cnblogs.com/wangting888/p/9701621.html
Copyright © 2011-2022 走看看