I have the following REST-style Spring MVC controller that accepts a skillId as a form parameter:
@RequestMapping(value = "{employeeId}/addSkill", method = RequestMethod.PUT)
public void updateEmployeeSkills(@PathVariable Integer employeeId, @RequestParam("skillId") Integer skillId) throws NoSuchRequestHandlingMethodException {
Employee employee = employeeDao.findEmployee(employeeId);
if (employee == null) {
throw new NoSuchRequestHandlingMethodException("No employee #" + employeeId, this.getClass());
}
Skill skill = skillDao.findSkill(skillId);
employee.getSkills().add(skill);
employeeDao.updateEmployee(employee);
}
And I have the client jQuery AJAX code as follows:
$("#add-skills-modal-form").submit(function() {
var $form = $(this);
// .serialize() to send the form input name-value pairs as params.
$.ajax({
// url can be obtained via the form action attribute passed to the JSP.
url: $form.attr("action"),
data: $form.serialize(),
type: "POST",
statusCode: {
404: function() {
alert("Employee not found");
},
500: function() {
alert("Failed to update Employee skills");
}
},
success: function() {
$('#add-skills-modal').modal('hide');
}
});
return false;
});
Now, there is no problem with the update into the database. The AJAX call works perfectly, however, the 404 error status code appears after the AJAX call successfully completes. So basically, an incorrect 404 error occurs, even though the AJAX call works perfectly. Also, the code in the success part of the AJAX call does not get executed which I am finding very peculiar.
Can anyone help me to fix this issue and understand why a 404 error is appearing when it shouldn't.