目的:前台使用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)
完成了
