Angular 如何使用HttpParams以查询字符串参数的形式在POST请求中传递数据
我使用HttpParams在Angular8中将一些数据作为查询字符串参数作为POST方法传递。在检查网络选项卡时,API没有使用查询字符串参数形成,请求头也不正确。请找到下面的代码库,如果有人对此有任何意见,请告诉我 组件技术Angular 如何使用HttpParams以查询字符串参数的形式在POST请求中传递数据,angular,post,query-string,http-parameters,Angular,Post,Query String,Http Parameters,我使用HttpParams在Angular8中将一些数据作为查询字符串参数作为POST方法传递。在检查网络选项卡时,API没有使用查询字符串参数形成,请求头也不正确。请找到下面的代码库,如果有人对此有任何意见,请告诉我 组件技术 this.data = { id: 21 } this.dataService.getData(this.data).subscribe( response => {console.log(response);} ) dataService.ts getDat
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});
}
post(api: string, request: any): Observable <any> {
return this.http.post<any>(api,request);
}
post(api: string, request: any): Observable <any> {
return this.http.post<any>(api, null, request);
}
httpService.ts
getData(inputParam) {
const params = new HttpParams().set('roleId', inputParam.id);
return this.httpService.post('getDataList', {params});
}
post(api: string, request: any): Observable <any> {
return this.http.post<any>(api,request);
}
post(api: string, request: any): Observable <any> {
return this.http.post<any>(api, null, request);
}
我们需要如下所示的带有查询参数的api
getDataList?roleId=21
当使用另一个GET方法进行测试时,上面的代码工作正常。唯一的问题是使用这个POST方法。我们需要为POST方法手动设置标题吗?任何人都可以在这个问题上提供帮助。Angular的HttpService上的post方法的接口是
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Observable<AxiosResponse<T>>;
post(url:string,data?:any,config?:AxiosRequestConfig):可观察;
第二个输入是post请求的主体,第三个输入是采用参数的任何其他AxiosRequestConfig
您是否尝试过:
httpService.ts
getData(inputParam) {
const params = new HttpParams().set('roleId', inputParam.id);
return this.httpService.post('getDataList', {params});
}
post(api: string, request: any): Observable <any> {
return this.http.post<any>(api,request);
}
post(api: string, request: any): Observable <any> {
return this.http.post<any>(api, null, request);
}
post(api:string,request:any):可观察{
返回this.http.post(api,null,request);
}
有人知道上述问题吗?