javascript - 成功删除请求后如何接收 ResponseEntity
问题描述
在我的数据库中删除一个实体后,我将一个 ResponseEntity 从我的后端(SpringBoot)返回到我的前端(Angular 10)。因此,我在 Angular 中发出了这个 HTTP 请求:
this.http.delete<ResponseEntity<string>>(this.productsPath + '/' + id).subscribe(data => console.log(data.statusCode + data.body));
为了接收响应,我创建了一个 ResponseEntity 接口:
interface ResponseEntity<T> {
headers: { [headerName: string]: string },
body: T,
statusCode: "OK" | "SERVER_ERROR" | "BAD_REQUEST", //etc
statusCodeValue: "200" | "500" | "400" | "404" //etc
}
现在在 SpringBoot 中,这是我的删除功能,它按预期工作:
@DeleteMapping(value = "/products/{id}")
public ResponseEntity<String> deleteProduct(@PathVariable String id) {
try {
productService.deleteProduct(Long.parseLong(id));
return ResponseEntity.status(HttpStatus.OK).body("Product deleted successfully.");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Product doesn't exist in Database.");
}
}
我的问题出在我想的 Angular 代码中,我真的不知道如何从 Spring 获取响应?删除实体有效,但我在 Angular 中登录到控制台的响应始终为null
,因此出现错误:ERROR TypeError: data is null
编辑:这个 SO 问题是我使用接口方法的原因:How to cast observable response to local object
解决方案
推荐阅读
- javascript - Cronjob 没有正确运行我的脚本
- javascript - 编写一个替代 String.prototype.replace() 的函数
- css - Firefox:第一次悬停闪烁
- node.js - 如何获取和存储客户端平台(操作系统和浏览器类型以及版本)、设备位置以及日期和时间
- android - Android 适配器在滚动时抛出 IndexOutOfBoundsException
- wpf - WPF 绑定到我的 DataContext 中的值
- docker - 用于运行二进制文件的多个 ENTRYPOINT
- javascript - 单击动态按钮后未显示信息(使用切换)
- c - 2D char 数组或 1D 数组到 char 指针哪个更快?
- mysql - 分离多个数据库使用的查找表