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