首页 > 编程语言 > 详细

springmvc 接受json参数的坑

时间:2019-04-30 00:52:14      阅读:193      评论:0      收藏:0      [点我收藏+]

 

构造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

springmvc 接受json参数的坑

原文:https://www.cnblogs.com/hfultrastrong/p/10793633.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!