1. 程式人生 > >前後臺JSON交互

前後臺JSON交互

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)
    
public 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(); } }
controller

前後臺JSON交互