I am working with Spring MVC 3.0.7. I send data using AJAX to my Spring controller but the spring controller is not called. Here is my Ajax call:
var mysaverowurl = '/app/user/save';
$.ajax({
url: mysaverowurl,
type: "POST",
data: postData,
contentType: 'application/json',
success: function () {
alert("success ");
}
});
postData contains {"name":"Franck","age":"94"}
Here is my Spring controller:
@Controller
@RequestMapping("/app/user")
public class myController {
@RequestMapping(value = "/save", method = RequestMethod.POST, headers = { "Content-type=application/json" })
@ResponseBody
public List<String> saveUser(HttpServletRequest request, Model model, @RequestBody User user) {
List<String> reponse = null;
// ...........
return reponse;
}
}
The controller is not called. But if remove @RequestBody
from the saveUser
signature:
public List<String> saveUser(HttpServletRequest request, Model model)
it gets called. Why @RequestBody
doesn't seem to work?
User class :
public class User{
private String name;
private String age;
public String getAge(){
return age;
}
public void setAge(String age){
this.age=age;
}
public String getName(){
return name;
}
public void setAge(String name){
this.name=name;
}
}
Here is a part of my pom file.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.8.1&</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Change your var mysaverowurl = '<c:url value="/app/user/save" />';
to
var mysaverowurl = '/app/user/save';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.