Home > Backend Development > PHP Tutorial > Detailed explanation of ajax passing list collection

Detailed explanation of ajax passing list collection

小云云
Release: 2023-03-21 19:48:01
Original
2774 people have browsed it

This article mainly shares with you the detailed explanation of ajax transfer list collection, hoping to help everyone.

1: ajax transfers List type data

js code:

  1. //声明list  
    var _list = [];  
    //放入string对象  
    for (var i = 0; i < 3; i++) {  
        _list[i]="tom";  
    }  
    $.ajax({  
        url : &#39;/ajax/test&#39;,  
        data : "list="+_list,  
        type : "POST",  
        success : function(data) {  
            alert(data);  
        }  
    });
    Copy after login


java code:

  1. @RequestMapping(value="test",method=RequestMethod.POST)  
    @ResponseBody  
    public String ajaxList(@RequestParam("list")List<String> strList){  
        for (String str : strList) {  
            System.out.println(str);  
        }  
        return "OK";  
    }
    Copy after login

2: ajax transfers List type data

Background I need to use a json parsing tool. I chose jackson

Import jackson dependency:

  1. <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.3</version>
    </dependency>
    Copy after login


js code:

  1. //声明list  
    var _list = [];  
    //创建两个user对象  
    var a= {};  
    a.name="tom";  
    a.age=23;  
    a.city="上海";  
    var b = {};  
    b.name="jack";  
    b.age=25;  
    a.city="安徽";  
    //将user放入_list  
    _list.push(a);  
    _list.push(b);  
    $.ajax({  
        url : &#39;/ajax/test1&#39;,  
        data : "list="+JSON.stringify(_list),  
        type : "POST",  
        success : function(data) {  
            alert(data);  
        }  
    });
    Copy after login

java code:

@RequestMapping(value="test",method=RequestMethod.POST)  
@ResponseBody  
public String ajaxList(@RequestParam("list")String userList) throws Exception{  
    //jackson对象  
    ObjectMapper mapper = new ObjectMapper();  
    //使用jackson将json转为List<User>
    JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);     
    List<User> list =  (List<User>)mapper.readValue(userList, jt);  
    return "OK";  
}
Copy after login

Three: When ajax passes any complex parameters, the background can read the data directly from the stream. Parse

js code:

//声明list  
var _list = [];  
//创建两个user对象  
var a= {};  
a.name="tom";  
a.age=23;  
a.city="上海";  
var b = {};  
b.name="jack";  
b.age=25;  
a.city="安徽";  
//将user放入_list  
_list.push(a);  
_list.push(b);  
$.ajax({  
    url : &#39;/querz/test&#39;,  
    data : JSON.stringify(_list),//这里需要json化  
    type : "POST",  
    success : function(data) {  
        alert(data);  
    }  
});
Copy after login
  1. java code:

  2. @RequestMapping(value="test",method=RequestMethod.POST)  
    @ResponseBody  
    public String ajaxList(HttpServletRequest request) throws Exception{  
        //从流中读取数据  
        BufferedReader br = request.getReader();  
        String str = "";  
        StringBuffer sb = new StringBuffer();  
        while((str = br.readLine()) != null){  
            sb.append(str);  
        }  
        ObjectMapper mapper = new ObjectMapper();  
        //使用jackson解析数据  
        JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);     
        List<User> list =  (List<User>)mapper.readValue(sb.toString(), jt);   
        System.out.println(list);  
        return "OK";  
    }
    Copy after login

The above is the detailed content of Detailed explanation of ajax passing list collection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template