About SpringMVC complete ajax function
/* $.post("ajax/Ajax1",{"name":"张三"},function(data){ $.each(data,function(i,a){ alert(a.name) }) }) */ $.post("ajax/Ajax2",{"name":"张三"},function(data){ alert(data) })
1. Add jsckson jar package
2. The method of adding in response to @ResponseBody json java object into the object.
The method may return value returned by the object set may be a string
@Controller @RequestMapping(value="ajax",produces = {"text/html;charset=utf-8"}) public class AjaxController { @RequestMapping("Ajax1") @ResponseBody public List<User> Ajax1(String name) { User user1=new User("张三1","1232",12,"hi"); User user2=new User("张三2","1232",12,"hi"); User user3=new User("张三3","1232",12,"hi"); User user4=new User("张三4","1232",12,"hi"); List<User> list=new ArrayList<User>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); return list; } @RequestMapping("Ajax2") @ResponseBody public String Ajax2(String name) { return "张三"; } }
Ajax situations where it is important for the string, then there will be garbled returned the following offer two kinds of solutions
1.@RequestMapping(value="ajax",produces = {"text/html;charset=utf-8"})
2 add the following code where resource allocation
.
This method is mainly based on the source code which has SpringMVC constructor parameter set coded by this method org.springframework.web.servlet.view.InternalResourceViewResolver