In springmvc, front-end ajax transfers multiple entity classes to the backend for receiving. springmvcajax
Objective: To import Multiple object classes to the background and insert them into the database using jquery ajax at the foreground
Entity class
private String saleNo ; private String drugNo ; private String drugName ; private String drugPrice ; private String drugNumber ; private String majorFunction ; private String spec ; private int saleType ; private String comment ;
In js, encapsulate the data to be passed in
Note that the preceding saleType is in int format and must comply
for (var i = 0; i < parseInt(operTimes)+1; i++) { var entity = new Object(); if(!isNull($("#the"+i+"drugNo").text())){ entity.saleNo = $("input[name='saleNo']").val() ; entity.drugNo = $("#the"+i+"drugNo").text() ; entity.drugName = $("#the"+i+"drugCommonName").text() ; entity.drugPrice = $("#the"+i+"Price").text() ; entity.drugNumber = $("#the"+i+"drugNumber").text() ; entity.majorFunction = $("#the"+i+"majorFunction").text() ; entity.spec = $("#the"+i+"spec").text() ; entity.saleType=parseInt($("#saleType").val()); entity.comment = "" ; ary.push(entity); } }
Ajax Method
In data: JSON. stringify (info), I started to use data: {"info": JSON. stringify (info)}. The object class cannot be received in the result background. Why?
$. Ajax ({contentType: "application/json; charset = UTF-8", type: "POST", traditional: true, dataType: "JSON", data: JSON. stringify (info), url: "/mv1/drugRetailDetail/ajaxInsDrugRetailDeInfo", // request url success: function (mess) {console. log ("ajaxInsDrugRetailDeInfo method succeeded"); alert ("inserted successfully! "); Window. location. href = "/mv1/paging/drugRetailInfo/qry";}, error: function (error) {alert ("ajaxInsDrugRetailDeInfo" + error );}});
Code accepted by the background
@ResponseBody @RequestMapping(value="/ajaxInsDrugRetailDeInfo",method=RequestMethod.POST) public String ajaxInsDrugRetailDeInfo(@RequestBody DrugSaleDetailEntity[] info)
Finished