ajax pass json array to controller

tags: ajax and Controller pass data

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

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

The controller receive 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","Successfully obtained information");
        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 receives:

@RequestMapping("/diagnoseInfo")
    @ResponseBody
    public Map<String, String> prescriptionData(@RequestBody List<Info> info){//Info is a class of single elements in json array
        Map<String, String> map = new HashMap<>();
        System.out.println(info);
        map.put("result","success");
        return map;
    }

Intelligent Recommendation

ajax traverse from backstage pass over the json array

This is just an array of foreground objects traverse json pass over the background, this is just a way there is a core may be displayed by the tag <c: foreach> </ c: foreach> to return bac...

springMVC, foreground ajax pass JSON array parameters, how to receive background

Background sections: // json package used: com.alibaba.fastjson...

Use Ajax to pass the JSON array to MVC background front end

Use Ajax to pass the JSON array to MVC background front end Front -end code: Background code:...

More Recommendation

SpringMvc in the foreground ajax pass json data background controller accept object is null

Recently, when using Ajax to pass the JSON format array to the controller layer, there is a problem with null. For details, please refer to   Finally, add a small supplement, after the data is pa...

Ajax -- controller layer pass json string to js, ​​date type display [Object object] solution

First, you must first introduce a tool class JsonDateValueProcessor.java Controller layer code When the string json thus obtained is passed to js, ​​the date type will not appear [Object object]....

Ajax transfer array to controller

1. View code 2. Controller code...

The front-end uses AJAX to pass the array to the back-end controller array parameters

The front-end uses AJAX to pass the array to the back-end controller array parameters There are two situations: 1. Array elements are arrays of basic types of data. 2. The array element is an array of...

Ajax pass array

Passing data in ajax can be used   Background reception can be used:   Note: The key is the $.ajax({traditional: true,}) setting, not the next String[] checkedIds can't receive the value. &n...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top