Javascript 通过ajax发送变量时获取exeption
尝试从数据库中删除用户时出错。 以下是我编写的代码: 这是服务器端:Javascript 通过ajax发送变量时获取exeption,javascript,jquery,ajax,spring-mvc,Javascript,Jquery,Ajax,Spring Mvc,尝试从数据库中删除用户时出错。 以下是我编写的代码: 这是服务器端: @RestController public class EmployeeRestController { @DeleteMapping( value = "/delete_user") public List<Employee> deleteEmployee(@RequestParam(value = "id") Integer id) { em
@RestController
public class EmployeeRestController {
@DeleteMapping( value = "/delete_user")
public List<Employee> deleteEmployee(@RequestParam(value = "id") Integer id) {
employeeService.deleteEmployee(id);
List<Employee> list = employeeService.getAllEmployees();
return list;
}
}
来自服务器端的错误:
DefaultHandlerExceptionResolver[0;39m [2m:[0;39m Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'id' is not present]
客户端的错误代码是400
我不擅长ajax、javascript和spring。很抱歉,如果问题很明显,请提前感谢。尝试使用PathVariable而不是RequestParam
@DeleteMapping(value = "/delete_user/{id}")
public List<Employee> deleteEmployee(@PathVariable("id") Integer id) {
employeeService.deleteEmployee(id);
List<Employee> list = employeeService.getAllEmployees();
return list;
}
}
@DeleteMapping(value=“/delete\u user/{id}”)
公共列表deleteEmployee(@PathVariable(“id”)整数id){
employeeService.deleteEmployee(id);
List List=employeeService.getAllEmployees();
退货清单;
}
}
你试过这个吗
function delete_row(id){
$.ajax({
type: "DELETE",
url: "/delete_user",
data: { id: id },
success: function (data) {
console.log(data);
},
error: function () {
}
});
}
或者直接绑定到url
function delete_row(id){
$.ajax({
type: "DELETE",
url: "/delete_user?id="+id,
success: function (data) {
console.log(data);
},
error: function () {
}
});
}
如果您没有发送名为
id
的查询参数,请尝试序列化JSONY。您拥有的是一个请求体,其中包含一个JSON对象,该对象包含一个名为id
的键。为什么您认为这会在他们的不起作用时起作用?
function delete_row(id){
$.ajax({
type: "DELETE",
url: "/delete_user?id="+id,
success: function (data) {
console.log(data);
},
error: function () {
}
});
}