我正在使用 hibernate spring MVC 和 JS 制作 CRUD 应用程序..现在我正在删除数据库中的一行..这是我的代码 我的 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));
},
我的 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);
}
它进入 Controller 并显示 jquery.min.js:4 POST http://localhost:8080/Spring3HibernateApp1/delete 500(内部服务器错误)
有什么建议吗??
最佳答案
HTTP 500
错误意味着,你的 Controller 更糟
用这个重构你的 Controller
@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);
}
和你的 JavaScript 代码是这样的
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));
},
关于javascript - 使用hibernate spring mvc检测数据库中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402483/