SpringMVC之通過ajax處理json陣列
阿新 • • 發佈:2020-11-27
一、導jar包:
二、controller.java:
@ResponseBody @RequestMapping(value="testJson") public List<Student> testJson(){ Student stu1 = new Student(1,"zs",21); Student stu2 = new Student(1,"zs",21); List<Student> students = new ArrayList<>(); students.add(stu1); students.add(stu2); return students; }
三、jsp:
<script type="text/javascript" src="js/jquery-1.5.1.js"></script> <title>Insert title here</title> </head> <body> <script type="text/javascript"> $(document).ready(function(){ $("#testJson").click(function(){ $.post( //伺服器地址 "SpringMVCController/testJson", //{"name":"zs"}, function(result){ for(var i=0;i<result.length;i++){ alert(result[i].id+"-"+result[i].name+"-"+result[i].age); } } ); }) }); </script> <input type="button" value="testJson" id="testJson">