jQuery:Ajax传实体到后台

我的后台需要接收的数据是一个Fence类的实例fence,fence包含了多个变量:fname,pos_x,pos_z,grade,radius

    @ControllerEndpoint(operation = "新增Fence", exceptionMessage = "新增Fence失败")
    @PostMapping("fence")
    @ResponseBody
    public FebsResponse addFence(@Valid Fence fence) {
        System.out.println("!!!"+fence);
        this.fenceService.createFence(fence);
        return new FebsResponse().success();
    }

使用Ajax从html中往后台传值时,Data为一个json串,后台解析需要的数据

    function addFenceToMysql(name,pos_x,pos_z,grade,radius){
        layui.use(['jquery'], function () {
            var $ = layui.jquery;
            $.ajax({
                url: "/fence",
                type: "POST",
                dataType: "json",
                data: {"Fname":name,"pos_x":pos_x,"pos_z":pos_z,"grade":grade,"radius":radius},
                success: function (data) {
                    console.log(name+','+pos_x+','+pos_z+','+radius);
                },
                error: function (err) {
                    console.log("bad error");
                }
            });
        });
    }

 

你可能感兴趣的:(前端)