angular - 在 Angular / HTTP POST 请求中作为空白传递的布尔数据
问题描述
我正在尝试将一些数据发布到 API,而我的布尔数据一直显示为空白。有没有人对可能导致这种情况的原因有任何想法?
const notifications: Array<Notification> = [
{
id: "all",
title: "All",
isActive: false
}
];
这是我interface
的数据:
export interface Notification {
id: string,
title: string,
isActive: boolean
}
现在,当我尝试通过HTTP POST请求传递这些数据时,它显示为:
notifications: [
{
"id": "all",
"title": "All",
"isActive": ""
}
];
我注意到isActive
当我通过 Safari/Chrome 检查调用时该属性为空白http.post()
,这导致我的 API 出现问题,因为数据看起来是空白而不是布尔值。
这是我将数据传递给 API 的方式:
this.http.post('/api/', {notifications: notifications}).subscribe((res:any) => {
console.log(res);
}
解决方案
尝试这个。在发送转换布尔值 toString 之前isActive.toString()
所以在制作api之前你可以做
notifications = {...notifications, isActive: notifications.isActive.toString() };
推荐阅读
- python - Python 和 matplotlib:绘图图例中上标和下标字符的相等垂直间距(使用 Arial)
- nsis - NSIS 安装程序如何跟踪每个用户的安装
- javascript - 如何将 Semver 与针对私有存储库的 NPM 依赖项一起使用?
- google-colaboratory - 使用 R 内核将私有数据导入 Google Colab 笔记本
- groovy - Groovy:无法解析类 org.apache.poi.xssf.usermodel.XSSFSheet
- google-translate - 是否可以自动翻译 Google Data Studio 中的内容?
- c# - 如何同时添加新成员和覆盖抽象成员?
- jooq - JOOQ 示例,例如 zvon.org,用于查询,尤其是连接查询
- python-3.x - 两个熊猫系列之间的匹配ID
- dns - 如何设置DNS,让一个gitlab页面的根域和子域指向同一个地址?