$.ajax({ type: 'delete', method: 'delete', url: '/BookShopSystem_war/deleteBookById/' + data.bookId, data: {"bookId": data.bookId}, dataType: 'json', success: function (result) { if (result.code == 0) { layer.msg(result.msg); table.reload("table") } else { layer.msg(result.msg) } } })@DeleteMapping("/deleteBookById/{bookId}") @ResponseBody public Map<String, Object> deleteBookById(@PathVariable("bookId") Integer bookId) { int deleteResult = adminService.deleteBookById(bookId); if (!(deleteResult == 1)) { System.out.println("删除失败"); } Map<String, Object> result = new HashMap<>(); result.put("code", 0); result.put("msg", "删除成功"); return result; }如果调用该接口的路径中还需要一个参数userId,那么应该如何修改
时间: 2024-03-06 08:51:37 浏览: 21
如果调用该接口的路径中需要传递userId参数,可以将该参数作为路径参数传递给后台,在Controller中使用@PathVariable注解获取该参数,例如:
前端代码:
```javascript
$.ajax({
type: 'delete',
method: 'delete',
url: '/BookShopSystem_war/deleteBookById/' + data.bookId + '/' + userId,
data: {"bookId": data.bookId},
dataType: 'json',
success: function (result) {
if (result.code == 0) {
layer.msg(result.msg);
table.reload("table")
} else {
layer.msg(result.msg)
}
}
});
```
后端Controller代码:
```java
@DeleteMapping("/deleteBookById/{bookId}/{userId}")
@ResponseBody
public Map<String, Object> deleteBookById(@PathVariable("bookId") Integer bookId, @PathVariable("userId") Integer userId) {
int deleteResult = adminService.deleteBookById(bookId);
if (!(deleteResult == 1)) {
System.out.println("删除失败");
}
Map<String, Object> result = new HashMap<>();
result.put("code", 0);
result.put("msg", "删除成功");
return result;
}
```
在Controller方法的路径变量中,添加@PathVariable注解,用于获取请求路径中的userId值。
阅读全文