【问题标题】:Angular: Request HTTP GET with many params [closed]Angular:使用许多参数请求 HTTP GET [关闭]
【发布时间】:2020-07-28 16:03:24
【问题描述】:

我有一个包含许多参数的 HTTP GET 请求(约 50 个 queryParams),

目前我提出了这个要求

  get(contact?: Contact): Observable<response[]> {
    let params: HttpParams = new HttpParams();
    if (contact.company !== null) params = params.append('company', contact.company);
    if (contact.lastname !== null) params = params.set('lastname', contact.lastname);
    if (contact.firstname !== null) params = params.set('firstname', contact.firstname);


    return this.http.get<response[]>(`url/contact/`, {
      params: params,
    });
  }

如何使用 50 个 queryParams 改进此请求?

谢谢

【问题讨论】:

  • 我建议你将所有这些参数添加到一个对象中,我相信这会更容易。
  • 您可以使用 post 方法并在正文中发送。这似乎是更可行的方法。

标签: angular httprequest


【解决方案1】:

您可以进行以下操作,

get(contact?: Contact): Observable<response[]> {

    let headers: HttpHeaders = new HttpHeaders();

    Object.keys(contact).forEach(key => {
        if(contact[key]){          
           headers = headers.append(key, contact[key]);
        }
    });

    return this.http.get<response[]>(`url/contact/`, {
           params: params,
    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多