前端请求:
//删除用户请求
export function deteleUser(id){
console.log('要删除的用户ID'+id);
return request({
url:'/user/'+id,
method:'delete',
// data:id
})
}
后端:
@DeleteMapping
@Log(title = "删除用户",businessType = "用户操作")
public ResponseEntity<Boolean> deleteById(Long id) {
System.out.println(id);
return ResponseEntity.ok(this.userService.deleteById(id));
}
运行以上代码报405
错误,request method ‘DELETE‘ not supported
1、报错原因
前端发送的rest 请求和后端的响应不匹配。
参数是从路径中获取的,前端发送请求时需要将参数一同带入到url中发送到后端
2、解决方案
检查前后端代码,发现出错在后端
使用@PathVariable
注解映射URL绑定的占位符并在@DeleteMapping
后增加请求参数
@DeleteMapping("{id}")
@Log(title = "删除用户",businessType = "用户操作")
public ResponseEntity<Boolean> deleteById(@PathVariable("id") Long id) {
System.out.println(id);
return ResponseEntity.ok(this.userService.deleteById(id));
}