Reputation: 97
i am making CRUD app using hibernate spring MVC and JS .. and now i am working in deleteing a row in the DB .. here is my code my js
deleteRow : function() {
$("input:checkbox:checked").each(bindContext(function(index, item) {
var str = $(item).attr("id");
str = str.substring(str.indexOf("_") + 1);
$.ajax({
url : '/Spring3HibernateApp1/delete',
type : 'POST',
data : {
"id" : str,
}
});
this.data.splice(str, 1);
this.deleteTable();
this.display();
}, this));
},
my controller
@RequestMapping(value = "/delete")
public @ResponseBody void doPostDelete(@RequestBody String id,Employee employee){
int idInt = Integer.parseInt(id);
employee.setEmpId(idInt-1);
employeeService.deleteEmployee(employee);
}
it enters to the controller and says jquery.min.js:4 POST http://localhost:8080/Spring3HibernateApp1/delete 500 (Internal Server Error)
any suggestions ??
Upvotes: 2
Views: 60
Reputation: 1185
HTTP 500
error mean, your controller worse
refactor your controller with this
@RequestMapping(value = "/delete/{id}" method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public void doPostDelete(@RequestBody Employee employee, @PathVariable("id") String id ) {
int idInt = Integer.parseInt(id);
employee.setEmpId(idInt - 1);
employeeService.deleteEmployee(employee);
}
and your JavaScript code like this
deleteRow : function() {
$("input:checkbox:checked").each(bindContext(function(index, item) {
var str = $(item).attr("id");
str = str.substring(str.indexOf("_") + 1);
$.ajax({
url : '/Spring3HibernateApp1/delete/' + id, // id is your patvariable
type : 'POST',
data : str // str must equal to employee json object
});
this.data.splice(str, 1);
this.deleteTable();
this.display();
}, this));
},
Upvotes: 1