0

我在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 方法手动设置标题?任何人都可以帮助解决这个问题。

4

1 回答 1

0

Angular 的 HttpService 上 post 方法的接口是

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

第二个输入是 post 请求的正文,第三个是任何其他带有参数的 AxiosRequestConfigs。

你有没有尝试过:

httpService.ts

post(api: string, request: any): Observable <any> {
    return this.http.post<any>(api, null, request);
}
于 2019-11-21T01:59:35.800 回答