I'm currently trying to send data from my jsp webpage view to my spring controller by using a Ajax POST json request but for some receiving the controller never seems to receive it. The strange thing is that it works when i use a GET request but since it is valuable data which need to be stored in a database, i would prefer to use POST instead.
The javascript code in my JSP view:
function ajaxEdit(){
var aData = oTable.fnGetData( iPos );//get data of the clicked row
var id = aData[0];
var data = {
id : id,
name : $('#editName').val(),
email : $('#editEmail').val(),
phoneNumber : $('#editPhoneNumber').val(),
address : $('#editAddress').val(),
city : $('#editCity').val(),
postalCode : $('#editPostalCode').val(),
funct : $('#editFunction').val(),
company : $('#editCompany').val(),
};
$.ajax({
type : "POST",
contentType: "application/json; charset=utf-8",
url : "ajaxEditPerson.htm",
dataType: "json",
data : JSON.stringify(data),
success : function(response) {
alert(response);
},
error : function(e) {
alert('Error: ' + e);
}
});
}
and my spring controller:
@RequestMapping(value = "ajaxEditPerson", method=RequestMethod.POST)
public @ResponseBody
String ajaxEditPerson(@RequestBody Person person, HttpServletRequest request){
System.out.println("received edit request: " + person.toString());
//Save edited item in database
personManager.addPerson(person);
return "okay";
}
What i'm i doing wrong here? The request doesn't seem to reach this method at all since the first print line is never executed. Also the error code alert in the javascript code always gives me this "Error: Object object" error. I've also added the following 2 dependencies to maven:
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
</dependencies>
EDIT:
I have changed the controller method arguments to:
@RequestMapping(value = "ajaxEditPerson", method=RequestMethod.POST)
public @ResponseBody
String ajaxEditPerson(@ModelAttribute("person") Person person, HttpServletRequest request, HttpServletResponse response){
System.out.println("received edit request: " + person.toString());
return "okay";
}
The print is now executed, so atleast the request has reached the method for sure. But the data that it included is all NULL and the ajax request also still receives an error reponse instead of a succesfull one....
EDIT2:
I can do both ajax GET and POST requests successfully now but without JSON. For some reason json doesn't seem to work. The corresponding method in the controller can receive the data but says its NULL. Is there anything else i should set up before using json? Something in the configuration files?
EDIT3:
Turned out that i needed to at the following line to my spring configuration :
<mvc:annotation-driven />
Everything is working fine now!