Ajax 三种方式
get
function fun1(){ $.get( "<%=basePath%>file/ajax", {"name":"zhansan","age":25}, function(data){ alert(data.name); }, "json" ); }
@RequestMapping(value = "/ajax",method=RequestMethod.GET) public @ResponseBody void testajax(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setCharacterEncoding("UTF-8"); try { response.getWriter().write("{\"name\":\"完成\"}"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
post
function fun2(){ $.post( "<%=basePath%>file/ajax2", {"name":"zhansan","age":25}, function(data){ alert(data.name); }, "json" ); }
@RequestMapping(value = "/ajax2",method=RequestMethod.POST) public @ResponseBody void testajax2(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.setCharacterEncoding("UTF-8"); try { response.getWriter().write("{\"name\":\"完成\"}"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
ajax
function fun3(){ $.ajax({ url:"<%=basePath%>file/ajax2", type:"POST", async:true, data:{"name":"lucy","age":20}, success:function(data){ alert( "Data Saved: " + data.name ); }, error:function(){ alert("Failed"); }, dataType:"json", }); }
原文:https://www.cnblogs.com/Jomini/p/9517584.html