1.传字符串
@RequestMapping("/t") @ResponseBody public ResultData t(String name,String pass){ Map<String,Object> map = new HashMap<String,Object>() ; map.put("name", name) ; map.put("pass", pass) ; return new ResultData(map) ; }
@RequestMapping("/t") @ResponseBody public ResultData t(@RequestParam("name") String name,@RequestParam("pass") String pass){ Map<String,Object> map = new HashMap<String,Object>() ; map.put("name", name) ; map.put("pass", pass) ; return new ResultData(map) ; }
<script type="text/javascript"> $("a").click(function(){ $.ajax({ url:"web/t", data:{name:"测试",pass:"123"}, success:function(data){ console.log(data) ; } }) }) ; </script>
2.传对象到后台
@RequestMapping("/t") @ResponseBody public ResultData t(Books book){ return new ResultData(book) ; }
<script type="text/javascript"> $("a").click(function(){ $.ajax({ url:"web/t", data:{name:"神话",writter:"张三"}, success:function(data){ console.log(data) ; } }) }) ; </script>
3.传集合或对象数组到后台
传集合不能用1,2的方式,用json字符串,后台在解析成集合
@RequestMapping("/t") @ResponseBody public ResultData t(String books ) { String book = books.replaceAll(""", """); JSONArray j = JSONArray.fromObject(book) ; List<Books> list = j.toList(j,Books.class) ; return new ResultData(list) ; }
<script type="text/javascript"> $("a").click(function(){ var books = [{name:"昏天魔王",writter:"南瓜大虾"},{name:"盖世英雄",writter:"真空泡"}] ; console.log(JSON.stringify(books)) $.ajax({ url:"web/t", data:{books:JSON.stringify(books)}, contentType: "application/json; charset=utf-8", dataType: "json", success:function(data){ console.log(data) ; } }) }) ; </script>