$.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-02-10 07:09:40 浏览: 70
微信小程序开发实践:图书借阅小程序实践心得与案例解析
如果调用该接口需要传递userId参数,可以将该参数作为请求参数传递给后台,在Controller中使用@RequestParam注解获取该参数,例如:
前端代码:
```javascript
$.ajax({
type: 'delete',
method: 'delete',
url: '/BookShopSystem_war/deleteBookById/' + data.bookId,
data: {
"bookId": data.bookId,
"userId": userId
},
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}")
@ResponseBody
public Map<String, Object> deleteBookById(@PathVariable("bookId") Integer bookId, @RequestParam 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方法的参数列表中,添加@RequestParam注解,用于获取请求参数中的userId值。
阅读全文