【发布时间】:2020-04-24 10:31:42
【问题描述】:
我有这个接收选择选项值的功能。 此外,我有 3 个选择选项(级别、设施、主机)。当我选择选项时,值将发送到此函数。
private optionFilterHandler = (option: string, key: string | number) => {
// the key can be level , facility , host
// whereas options are the values of keys
// for level option values are (debug , warning ,info etc)
// similarly for host and facility have options values
if (option || key) {
this.setState({
option: option,
key: key,
},
this.filterHandler
); // callback function
}
然后作为选项和键存储在状态中
问题是我需要将所有三个选项都发送到 API 例如:API调用应该是这样的
private filterHandler = async () => {
const response: Response = await fetch(
'http://egrde-tvm-aso1.de.egr.lan:3000/api/v1/search',
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
facility: this.state.option,
level: this.state.option,
host: this.state.option
});
});
}
那么我应该如何发送 json.Stringify({}) 中的所有三个选项,因为我只得到一个选项值 每次我选择选项时,如何检查选项值是否属于级别、设施或主机。
【问题讨论】:
标签: json reactjs typescript api filter