如何在 Angular 8 应用程序中将数组发送到 get 请求?

use*_*483 0 angular

我正在尝试在 get 请求中将 id 列表发送到服务器,我正在执行以下操作:

public loadTripsByIds(tripsIds:number[]): Observable<any> {              
    let params = new HttpParams();
    params = params.append('tripsIds', tripsIds.join(', '));    
    return this.http.get<TripObj[]>(`${this.baseUrl}/Trips/ByIds/Get`, {params: params});                        
  }
Run Code Online (Sandbox Code Playgroud)

在服务器 api 代码(其余)中,我定义了列表:

@GET
@Path("Trips/ById/Get") 
@Produces("application/json")
public List<Trips> loadTripsById(@QueryParam("tripsIds") final List<String> tripsIds) {
Run Code Online (Sandbox Code Playgroud)

我在服务器中实际得到的是一个包含 1 个项目(字符串类型)并以逗号分隔的列表。例如“10001、10002”。我可以轻松地解析服务器端的字符串,但寻找正确的方法将列表发送到服务器,其中每个元素将是 id。

谢谢。

use*_*483 5

为了解决这个问题,我现在发送参数数组,如下所示:

let params = new HttpParams();
    for (let id of tripsIds) {
      params = params.append('tripIds', id); 
    }  
Run Code Online (Sandbox Code Playgroud)