我在Angular8中使用HttpParams将一些数据作为查询字符串参数作为POST方法传递。在检查网络选项卡时,API 未使用查询字符串参数形成,请求标头也不正确。请找到下面的代码库,如果有人对此有任何意见,请告诉我。
组件.ts
this.data = {
id: 21
}
this.dataService.getData(this.data).subscribe(
response => {console.log(response);}
)
数据服务.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 方法手动设置标题?任何人都可以帮助解决这个问题。