前台提交整个表单数据,后台实体类接收

function su() {
    var formData=$("#form").serializeObject();   //这个把表单按对象类型序列化
    $.ajax({
        url: '<%=path%>/echarge/discount/SaveOrUpdate',
        type: "POST",
        dataType: "json",
        contentType: 'application/json',
        data:JSON.stringify(formData),
        success: function (res) {
            console.log(res)
            if(res.code==0){

                $("input").attr('disabled','disabled');

               alert('已生效');
            }else{
                alert("操作失败")
            }
        },
    })

}

后台:

 

@RequestMapping("/SaveOrUpdate")
@ResponseBody                          //这里用实体类接收
public Map SaveOrUpdate(@RequestBody Discount discount, HttpServletRequest request, HttpSession session) {
    Map map=new HashMap<>();
    try {
        int c = -1;
        discount.setFlag("1");
        c = discountService.saveOrUpdate(discount);
        if (c > 0) {
            map.put("code",0);
            map.put("id", CommonMap.map.get("RID"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        map.put("code",1);
    }
    return map;
}

你可能感兴趣的:(java)