Front-End Code:
<formID= "Person_add"Method= "POST"Action= "User"> <Tableclass= "Table_add"> <TR> <TD> </TD> <TD>Name</TD> <TD><inputname= "Name"value=""/></TD> <TD>Address</TD> <TD><inputname= "Address"value=""/></TD> </TR> <TR> <TD></TD> <TD>Name</TD> <TD><inputname= "Name"value=""/></TD> <TD>Address</TD> <TD><inputname= "Address"value=""/></TD> </TR> <TR> <TD></TD> <TD>Name</TD> <TD><inputname= "Name"value=""/></TD> <TD>Address</TD> <TD><inputname= "Address"value=""/></TD> </TR> <TR> <TD></TD> <TD>Name</TD> <TD><inputname= "Name"value=""/></TD> <TD>Address</TD> <TD><inputname= "Address"value=""/></TD> </TR> <TRAlign= "Center"> <TDcolspan= "5"> <aID= "Menu_submit"href= "Javascript:onsubmit ()">Submit</a> <aID= "Menu_no"href= "Javascript:closedialog ()">Shut down</a> </TD> </TR> </Table></form>
Controller code:
@PutMapping publicvoid addUser (@RequestBody list<person> Persons) { System.out.println (persons.tostring ()); }
If a controller is used to receive parameters in the background, then the parameters must be passed in the following format:
[{"Name": "1", "Address": "A1"},{"Name": "2", "Address": "B1"},{"Name": "3", "Address": "C1"},{"name": "4", "Address": "D1 "}]
First, the form is serialized,
$.serializeobject =function (form) {var o= {}; $.each (Form.serializearray (), function (index) {if(o[ This[' Name ']]) { if($.isarray (o[ This[' Name ']]) {o[ This[' name ']]. Push This[' Value ']); } Else{o[ This[' name ']] = [o[ This[' name ']], This[' Value ']]; } } Else{o[ This[' name ']] = This[' Value ']; } }); returno; };
The serialized form is an object that, through invocation, gets an object
var Serializearray = $.serializeobject ($ ("#person_add"));
Decompose the object into the JSON string we need, json.stringify () Convert the object to a string
//to calculate the maximum length of the array inside the JSON, you must have this step for(varIteminchSerializearray) { varTMP = $.isarray (Serializearray[item])? Serializearray[item].length:1; Vcount= (tmp > Vcount)?Tmp:vcount; } if(Vcount > 1) { varJSONDATA2 =NewArray (); for(vari = 0; i < Vcount; i++) { varJsonobj = {}; for(varIteminchSerializearray) {Jsonobj[item]=Serializearray[item][i]; } jsondata2.push (Jsonobj); } jsonparams=json.stringify (JSONDATA2); } Else{jsonparams=json.stringify (Serializearray); }; Console.info (jsonparams);
The jsonparams we get is the JSON string we need.
Upload directly via Ajax:
$.ajax ({ "user", "PUT" , "Application/json; Charset=utf-8", data: Jsonparams, function (res) { alert (res); } })
"Springmvc" uses jquery to receive the front-end incoming list object