首先是发送AJAX请求的html页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>AJAX</title> <script type="text/javascript" src="/lib/jquery-3.2.1.min.js"></script> <script type="text/javascript"> var saveData = {"en":"sss", "ch":"げ啊爲", "dt":"2012-01-02 03:04:05", "no":"-3.1415926"}; function json_obj() { $.ajax({ type:"post", url:"/ajax/json_obj", dataType:"json", data:saveData, success:function(data) { alert(data); }, error:function(data) { alert(‘error‘); } }); } function json_str() { $.ajax({ type:"post", url:"/ajax/json_str", dataType:"json", contentType : ‘application/json‘, data:JSON.stringify(saveData), success:function(data) { alert(data); }, error:function(data) { alert(‘error‘); } }); } </script> </head> <body> <input id="a" value="b" type="hidden" /> <button onclick="json_obj()">json_obj</button><br><br> <button onclick="json_str()">json_str</button><br><br> </body> </html>
然后是控制器和用于和参数绑定的实体类
/** ‘package‘ and ‘import‘ ingore */ @Controller @RequestMapping("ajax") public class AjaxController { private static final Logger log = LogManager.getLogger(AjaxController.class); @RequestMapping(value = "json_obj") @ResponseBody public String jsonobj(Data data) { log.info("请求开始"); log.info("请求成功"); return "success"; } @RequestMapping(value = "json_str") @ResponseBody public String jsonstr(@RequestBody DataS data) { log.info("请求开始"); log.info("请求成功"); return "success"; } } class Data { private String en; private String ch; private Date dt; private double no; /** getter and setter ignore */ } class DataS { private String en; private String ch; private String dt; private String no; /** getter and setter ignore */ }
时间: 2024-10-05 04:45:07