如何使用HttpParams将POST请求中的数据作为angular中的查询字符串参数传递

时间:2019-10-09 06:59:42

标签: angular post query-string http-parameters

我正在使用HttpParams将一些数据作为查询字符串参数作为Angular8中的POST方法传递。在检查网络标签时,API并不包含查询字符串参数,并且请求标头也不正确。请找到以下代码库,并让我知道是否有人对此进行输入。

component.ts

this.data = {
 id: 21
}
this.dataService.getData(this.data).subscribe(
response => {console.log(response);}
)

dataService.ts

getData(inputParam) {
const params = new HttpParams().set('roleId', inputParam.id);
return this.httpService.post('getDataList', {params});
}

httpService.ts

post(api: string, request: any): Observable <any> {
return this.http.post<any>(api,request);
}

在检查网络标签时,api如下所示

getDataList

我们需要具有以下查询参数的api

getDataList?roleId=21

上面的代码在使用另一个GET方法进行测试时运行良好,唯一的问题是此POST方法。我们是否需要为POST方法手动设置标题?任何人都可以在这个问题上提供帮助。

1 个答案:

答案 0 :(得分:0)

Angular的HttpService上的post方法的接口是

post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Observable<AxiosResponse<T>>;

第二个输入是发布请求的正文,第三个输入是接受参数的任何其他AxiosRequestConfigs。

您是否尝试过:

httpService.ts

post(api: string, request: any): Observable <any> {
    return this.http.post<any>(api, null, request);
}
相关问题