In springmvc, front-end ajax transfers multiple entity classes to the backend for receiving. springmvcajax

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.