springMVC接受對象實體並且對象實體里面又有對象集合方式:
Ajax:
function add(){ var orders = [ { orderNo : "H222255" }, { orderNo : "H222256" } ] var user = { username : "劉亞超", password : "ab0715", orderList : orders } debugger; $.ajax({ url: '/store/api/test/add.json', type: "POST", data: JSON.stringify(user),//將對象序列化成JSON字符串 dataType: "json", contentType : 'application/json;charset=utf-8', //設置請求頭信息 async: false, success: function (result) { debugger; }, error: function (xhr, ajaxOptions, thrownError) { debugger; alert("出錯了"); } }) }
說明:
1.
data: JSON.stringify(user),//將對象序列化成JSON字符串
dataType: "json",
contentType : 'application/json;charset=utf-8', //設置請求頭信息
缺一不可,否則會報錯。
2.
add.json沒有錯誤;
如果是add.html后綴,springmvc默認會采用[text/html]編碼。所以,后綴使用別的后綴或者,不用后綴就可以了。
Controller接受:
@RequestMapping("/add") @ResponseBody public BaseResponse test(@RequestBody UserParam userParam){ //用戶重置 userParam.setUsername("李雪雷"); userParam.setPassword("66666"); return BaseResponse.successCustom().setData(userParam).build(); }
說明:
@RequestBody不能去掉,否則會報錯