如何在角度应用程序中将查询参数传递给服务中的REST API?

问题描述 投票:1回答:4

我正在尝试将服务中的查询参数传递给REST API。

我应该如何传递给API的示例。(预期)

http://localhost:2000/world/123456789/avengers?type=fruits&fields=_all

尝试过如下:

     all(countId) {
        const headers: HttpHeaders = new HttpHeaders({
            "_id" : countId, 
            "content-type" : "application/json"
        });
        let params = new HttpParams();
        params = params.append("type", "fruits");
        params = params.append("fields", "_all");
        const options = {
            headers: headers,
            params: params
        };
        return this.http.put ( "http://localhost:2000/world/123456789/avengers", options )
    }

但我无法通过它们有查询参数。我会这样做吗?

enter image description here

angular typescript api service query-parameters
4个回答
1
投票

您正在发送您的选项作为请求有效负载,就像您发现的那样。如果您没有有效负载,可以通过null

all(countId) {
  // ....
  return this.http.put("...", null, options)
                              ^^^^
}

1
投票

因为HTTP PUT/POST在URL中发送没有附加查询字符串的正文(您可以使用某些库来构建查询字符串),因此您需要构建您的URL和选项

 * @param url The endpoint URL.
     * @param body The resources to add/update.
     * @param options HTTP options
     *
     * @return An `Observable` of the response, with the response body as a JSON object.
     */
    put(url: string, body: any | null, options?: {}


all(countId) {
        const headers: HttpHeaders = new HttpHeaders({
            "_id" : countId, 
            "content-type" : "application/json"
        });

        const options = {
            headers: headers,
            params: params
        };
        return this.http.put ( "http://localhost:2000/world/123456789/avengers??type=fruits&fields=_all", options)
    }

0
投票

你可以把它们作为Params发送

const headers: HttpHeaders = new HttpHeaders({
            "_id" : countId, 
            "content-type" : "application/json"
        });
let params = new HttpParams().set('type',fruits).set('fields',_all);
return this.http.put ( "http://localhost:2000/world/123456789/avengers",{ params: params, headers: headers})

或者您可以发送选项

let options: HttpOptions;
options.headers = headers;
options.params = params;
return this.http.put ( "http://localhost:2000/world/123456789/avengers", options )

0
投票

/ **********这是我的组件文件*********** /

page: any;
error: {};
orderObj: {};

ngOnInit(){
  const type: string = this.route.snapshot.queryParamMap.get('type');
  const fields: string = this.route.snapshot.queryParamMap.get('fields');
  this.orderObj = {'type' : type, 'fields' : fields}

  this.route.paramMap.pipe(
  switchMap((params: ParamMap) =>
    this.cmspageService.getPage(params.get('slug'), this.orderObj)
  )
  ).subscribe(
  (data: any) => this.page = data,
  error => this.error = error
  );
}

/ **********这是我的服务文件*********** /

public queryStringUrl : string

getPage(slug: string, data:object){
   if(data){
      this.queryStringUrl = '?';
      let i = 0; 
      for (var p in data) {
       if (data.hasOwnProperty(p)){
        if(i != 0)
        this.queryStringUrl += '&';
        this.queryStringUrl +=  p + '=' + data[p];
      }
      i++;
    }
    //return this.queryStringUrl;
    // alert(this.queryStringUrl);
}

return this.http.get<Page>('http://localhost/dev/blogger/api/page/' + slug + this.queryStringUrl,)
.pipe(
  catchError(this.handleError)
);
}

参考链接:qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.