Reception and transmission parameter using Ajax
1, the return type is text type Controller manner.
@Controller public class UserAnnotation { @RequestMapping("info") @ResponseBody public String list(String name) { System.out.println(name); return ""; } }
Jquery call the front desk with the following codes
<script type="text/javascript"> $.get("info",{"name":"张三"},function(data){ alert(data); }) </script>
2, Controller returns the object type manner JSON
2.1, the first to import the package resolved jackson
2.2, together with the response method in @ResponseBody: json object into the java object.
2.3, the return value method may be a collection of strings objects.
code show as below:
@Controller public class UserAnnotation { @RequestMapping ( "info" ) @ResponseBody // json Java objects into an object, use the jar package jackson
public the User List () { the User User the User new new = ( "Joe Smith", 18) return User ; } }
Jquery call the front desk with the following codes
<script type="text/javascript"> $.get("info",function(data){ alert(data.uname+" "+data.age); }) </script>
ajax returned to a string garbled solution
The reason is that distortion can be seen from the figure, provided in the source coding format to ISO-8859-1
Solution
The first
Springmvc profile configuration following code:
<!--开启注解驱动AnnotationHandlerMapping --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg index="0" value="utf-8"></constructor-arg> </bean> </mvc:message-converters> </mvc:annotation-driven>
The second
Add encoding @RequestMapping provided as follows
@RequestMapping(value="info" ,produces="text/html;charset=UTF-8")