Angular HTTP参数返回数组,而不是在get请求中重复



我试图在Angular get请求中为单个值设置多个params值,但它为这里的值返回数组是我的代码。

let params = new HttpParams;
let prefectures = [01, 02] //dynamic array of numbers
for(let pre of prefectures){
params = params.append('prefecturesCode', pre);
}

这是请求代码

return this.http
.get(this.replaceBaseUrl(url), {
headers,
params: params
})

我得到这个网址

http://localhost:3003/endpoint?prefecturesCode=02,03

但我需要的是像这个

http://localhost:3003/endpoint?prefecturesCode=02&prefecturesCode=03

我做错了什么?

我认为如果您需要在查询参数中传递一个值列表,正确的方法是

http://localhost:3003/endpoint?prefecturesCode=02,03

例如,在node.js中,当Express解析请求中的查询参数时,它将存储在params对象中,该对象具有带参数名称的属性。因此,如果您两次发送相同的参数,您将只得到值03或一个错误。

希望这能帮助到你。

最新更新