How to pass an array within a query string in HttpClient?

前端 未结 11 873
面向向阳花
面向向阳花 2021-02-02 09:03

This is my array of actors:

[\'Elvis\', \'Jane\', \'Frances\']

How to pass this array within a query strin

相关标签:
11条回答
  • 2021-02-02 09:21
    const actors = ['Elvis', 'Jane', 'Frances'];
    let params = new HttpParams();
    for (const actor of actors) {
      params = params.append('actors', actor);
    }
    
    this.http.get(url, { params: params }); 
    
    0 讨论(0)
  • 2021-02-02 09:22

    I think the best way is to add them to parameters as a string and have your back-end convert it back to an array or list.

    let actorList = ['Elvis', 'Jane', 'Frances']
    let params = new HttpParams();
    params = params.append('actors', actorList.join(', '));
    this.http.get(url, { params: params });
    
    0 讨论(0)
  • 2021-02-02 09:23
    let actorsArray = ['Elvis', 'Jane', 'Frances'];
    this.http.get(url, { params: { actors: actorsArray } });
    
    0 讨论(0)
  • 2021-02-02 09:30

    You can do the following:

    this.http.get(url, { params: { "actors[]": actorsArray } });
    
    0 讨论(0)
  • 2021-02-02 09:31

    Here is a simple way to do it:

    this.http.get(url, { 
        params: ['Elvis', 'Jane', 'Frances'].reduce((accumulator, name) => accumulator.append('names', name), new HttpParams())
    });
    
    0 讨论(0)
提交回复
热议问题