前後臺JSON交互
阿新 • • 發佈:2017-07-21
exceptio 後臺 前後臺 lose utf ase str pos control
基礎環境 spring+mybatis
function postUser(){ var userName=$("input[name=‘userName‘]").val(); var age=$("input[name=‘age‘]").val(); var model=new Object(); model.userName=userName; model.age=age; $.ajax({ type :前臺‘post‘, url : ‘<%=basePath%>user/postUser‘, //設置contentType類型為json contentType : ‘application/json;charset=utf-8‘, //json數據 data : JSON.stringify(model), //請求成功後的回調函數 success : function(data) { alert(data.result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(‘服務器請求超時!‘); }, }); }
@RequestMapping(value="/postUser",method=RequestMethod.POST)controllerpublic void postUser(@RequestBody String objJson,HttpServletRequest request,HttpServletResponse response){ System.out.println("postUser"); String result = "{\"result\":\"success\"}"; try { System.out.println(objJson); response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.write(result); } catch (Exception e) { e.printStackTrace(); } }
前後臺JSON交互