如何将 JSON 请求主体的值设置为数组而不是字符串
How to set the value of a JSON request body to an Array instead of String
我有 ff。获取值并向 API
发送请求的代码
this.apiService.remove(this.toRemoveItem.itemSelected).subscribe((res: any) => {
this.someProcessService.save(null);
}, error => {
this.someProcessService.errorScenarioFlow(error);
}
服务就在这里
export class DummyApiService {
......
remove(Item: string) {
const body = {
Item
};
return this.http.post(this.mock, body, {
headers: this.headerTest(localStorage.getItem('headers'))
});
}
.....
}
它的作用是输出
{
"Item" : "Selected Item"
}
但我需要的输出是
{
"Item" : ["Selected Item"]
}
其中 Item 是项目数组。但出于某种原因,它一直在所选项目的值的开头和结尾添加双引号,因此使 Item 成为字符串而不是数组。我如何让它输出到项目数组而不是项目字符串。
const body = {
Item: [Item]
};
我不明白目的,但你总是可以破坏一个对象并映射它:
remove(Item: string) {
const body = { Item };
const { item, ...headers } = this.headerTest(localStorage.getItem('headers'));
return this.http.post(this.mock, body, { headers: [item] });
}
我有 ff。获取值并向 API
发送请求的代码this.apiService.remove(this.toRemoveItem.itemSelected).subscribe((res: any) => {
this.someProcessService.save(null);
}, error => {
this.someProcessService.errorScenarioFlow(error);
}
服务就在这里
export class DummyApiService {
......
remove(Item: string) {
const body = {
Item
};
return this.http.post(this.mock, body, {
headers: this.headerTest(localStorage.getItem('headers'))
});
}
.....
}
它的作用是输出
{
"Item" : "Selected Item"
}
但我需要的输出是
{
"Item" : ["Selected Item"]
}
其中 Item 是项目数组。但出于某种原因,它一直在所选项目的值的开头和结尾添加双引号,因此使 Item 成为字符串而不是数组。我如何让它输出到项目数组而不是项目字符串。
const body = {
Item: [Item]
};
我不明白目的,但你总是可以破坏一个对象并映射它:
remove(Item: string) {
const body = { Item };
const { item, ...headers } = this.headerTest(localStorage.getItem('headers'));
return this.http.post(this.mock, body, { headers: [item] });
}