zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    黄金作为货币的本质
    万有引力和做功。
    Arp攻击实战
    Android SDK下载项的说明
    宇宙、事象
    事件视界
    微积分
    金融的三大基础货币,股票,期货。
    pos机的热敏纸尺寸
    去除text历史记录
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/10793633.html
Copyright © 2011-2022 走看看