通过ajax处理json数据
1.jar包
2.@Responsebody
//Json @ResponseBody //告诉springmvc此时的返回值不是一个view,而是ajax调用的返回值json数组 @RequestMapping(value = "testJson") public List<Student> testJson(Student student){ Student student1 = new Student(1,"zs",23); Student student2 = new Student(2,"ls",25); Student student3 = new Student(3,"ww",63); List<Student> students=new ArrayList<>(); students.add(student1); students.add(student2); students.add(student3); return students; }
前台:服务端将返回值结果以json数组的形式传给了result。
<script type="text/javascript"> $(document).ready(function () { $('#textJson').click(function () { //通过ajax请求springmvc $.post( "SpringMVCHandler/testJson",//服务器地址 //{"name":"zs"}, function (result) {//服务器处理完毕后的回调函数List<Student> student for(var i=0;i<result.length;i++){ alert(result[i].id+"-"+result[i].name+"-"+result[i].age); } } ); }) }) </script>