构造json数据时候js对象中的值 必定要用 "" 双引号,不能用单引号,由于转成字符串后,到后台进行解析时,由于java认为单引号是单字符 ,转不成对应的字符串,因此会报错!java
以下正确:ajax
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); } }); }
后台接收json
@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