ajax pass json array to controller

1. The json array transmitted by ajax is a bit different from the json data that is often transmitted. The usual json data is:

$.ajax({
    
    
                url: '[[@{/diagnoseInfo}]]',
                method: 'post',
                data: jsonData,
                dataType: 'JSON',
                success: function (res) {
    
    
                    console.log(res);
                },
                error: function (data) {
    
    
                    console.log(data);
                }
            });

The controller reception is:

@RequestMapping("/diagnoseInfo")
    @ResponseBody
    public Map<String, String> diagnoseInfo(String name, String sex){
    
    
        Map<String, String> map = new HashMap<>();
        System.out.println(name);
        System.out.println(sex);
        map.put("result","获取信息成功");
        return map;
    }

2. Now you want to pass the json array:

$.ajax({
    
    
                url: '[[@{/diagnoseInfo}]]',
                method: 'post',
                data: JSON.stringify(jsonArray),
                dataType: 'JSON',
                contentType: "application/json;charset=utf-8",
                success: function (res) {
    
    
                    console.log(res);
                },
                error: function (data) {
    
    
                    console.log(data);
                }
            });

The controller reception is:

@RequestMapping("/diagnoseInfo")
    @ResponseBody
    public Map<String, String> prescriptionData(@RequestBody List<Info> info){
    
    //Info是json数组单个元素的类
        Map<String, String> map = new HashMap<>();
        System.out.println(info);
        map.put("result","成功");
        return map;
    }

Guess you like

Origin blog.csdn.net/xlk_1996/article/details/103746489