英文:
sending array as a query param in angular post request
问题
以下是您的Angular post请求的代码部分的中文翻译:
this._http.post(
RESOURCE_URL, // 我的查询URL
{
params: {
houses: ids
}
}
);
变量 ids
是一个发送到后端的数字数组,例如 ids = [1,2]
。
我的问题是,当我发送请求时,它会以以下方式发送:RESOURCE_URL?houses=1&houses=2
,这不是我想要的。
我想要的格式更像是这样:RESOURCE_URL?houses=[1,2]
。
现在有人可能会建议为什么我不将它传递给请求体。问题是,出于项目特定的原因,我不能这样做。需要太多工作,而且我没有权力决定是否要深入研究这个问题。
是否有任何方法可以实现这个?因为我已经查看了这个主题和这个主题,似乎这是不可能的。
<details>
<summary>英文:</summary>
I have the following angular post request :
```typescript
this._http.post(
RESOURCE_URL, // my query URL
{
params : {
houses: ids
}
}
);
the variable ids
is an array of numbers that I send to the back end, with ids = [1,2]
for instance.
my issue is, as I send my request, it is sent as the following RESOURCE_URL?houses=1&houses=2
, which is not what I want.
I'm trying to have it more like this : RESOURCE_URL?houses=[1,2]
.
Now one may suggest why I don't just pass it on to the request body. Problem is, for prject-specific reasons, I cannot. Too much work would be required and I do not have power to decide whether I should dive into it or not.
is there anyway this can be done ? because haveing looked at this topic and also this one, and it seems like it's just not possible.
答案1
得分: 3
可以的,这只是 Angular 不是以这种方式处理的而已。所以,只需手动操作。
this._http.post('我的链接', {
params: {
houses: `[${ids.join(',')}]`
}
})
英文:
It's possible, it's just not the way Angular does this. So, just do it manually.
this._http.post('my_url', {
params: {
houses: `[${ids.join(',')}]`
}
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论