spring mvc ajax
controller 处理请求的代码
@ResponseBody @RequestMapping(value = "ajax", produces = "application/json") public String ajax(HttpServletRequest request,HttpServletResponse response) { String getString=request.getParameter("ajaxSendMessage"); System.out.println(getString); response.setContentType("application/json"); return "{\"answer\":\"" + getString + "\"}"; }
ajax代码
<script type="text/javascript"> $.ajax({ type:'POST', url:'/helloworld/helloworld/ajax', //请求的url data:{ ajaxSendMessage:'I was posted from ajax' }, success:function(msg){ alert(msg.answer); } }) </script>