在angular获取httpclient请求中传递params

时间:2018-04-26 11:58:42

标签: angular angular-httpclient

我会通过header或params设置数据并使用get方法。

我创建了我的服务。

public openFile(pathFile) {
  let url='/download/';
  let mockUrl; 
  let file= new HttpHeaders();
  file=pathFile
  return this.httpClient.get(url,file);
}

VS代码中的问题我收到此错误:

Argument of type 'HttpHeaders' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'

当我尝试运行该服务时,服务器给我这个错误:

{timestamp: 1524743028303, status: 400, error: "Bad Request", exception: "org.springframework.web.bind.MissingServletRequestParameterException", message: "Required String parameter 'pathFile' is not present", …}

1 个答案:

答案 0 :(得分:0)

编辑:我错过了另一个错误,因为我复制了您的示例并且只更改了一行。

查看httpClient.get的行。您不直接传入标题,但传入一个具有标题字段的对象。

public openFile(pathFile) {
    let url='/download/';
    let headers= new HttpHeaders();
    headers.set('file', pathFile);
    return this.httpClient.get(url, { headers: headers });
}

正如@infodev在评论中提到的,你可以省略HttpHeaders类。然后它看起来像这样:

public openFile(pathFile) {
    let url='/download/';
    return this.httpClient.get(url, { headers: { file: pathFile } });
}