springmvc 接受json参数的坑

构造json数据时候js对象中的值 一定要用 "" 双引号,不能用单引号,因为转成字符串后,到后台进行解析时,因为java认为单引号是单字符 ,转不成对应的字符串,所以会报错!

如下正确:

function insertByEntity() {
        var url = "/tuser/insertByEntity";
        var entity = {
            nickname: "nickname",
            realname: "realname",
            username: "username",
            userpass: "userpass",
            age: "age",
            sex: "sex",
            tel: "tle"
        };
        $.ajax(url, {
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(entity),
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            },
            success: function (data) {
                console.log(data);
            }
        });
    }

后台接收

@RequestMapping(value = "insertByEntity",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //application/json;charset=UTF-8
produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public RetJson insert(@RequestBody TUser entity) {}
application/json;charset=UTF-8

猜你喜欢

转载自www.cnblogs.com/hfultrastrong/p/10793633.html
今日推荐