目的:前台使用jquery的ajax把多个实体类传入后台,插入数据库
实体类
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 ;
在js中,把要传入的数据封装
这里注意,上面的saleType为int格式,需要符合
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方法
在 data:JSON.stringify(info)这里,开始我使用 data:{ "info":JSON.stringify(info) },结果后台接收不到实体类,不知什么原因
$.ajax({ contentType:"application/json;charset=utf-8", type:"POST", traditional:true, dataType:"JSON", data: JSON.stringify(info) , url:"/mv1/drugRetailDetail/ajaxInsDrugRetailDeInfo",//请求的地址 success:function(mess){ console.log( "ajaxInsDrugRetailDeInfo方法成功了" ); alert("插入成功!"); window.location.href="/mv1/paging/drugRetailInfo/qry"; }, error:function(error){ alert("ajaxInsDrugRetailDeInfo"+error); } });
后台接受的代码
@ResponseBody @RequestMapping(value="/ajaxInsDrugRetailDeInfo",method=RequestMethod.POST) public String ajaxInsDrugRetailDeInfo(@RequestBody DrugSaleDetailEntity[] info)
完成了