将数组作为查询参数发送到Angular的POST请求中

huangapple go评论57阅读模式
英文:

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&amp;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(&#39;my_url&#39;, {
  params: {
    houses: `[${ids.join(&#39;,&#39;)}]`
  }
})

huangapple
  • 本文由 发表于 2023年6月27日 20:27:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定