Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何使用HttpParams以查询字符串参数的形式在POST请求中传递数据_Angular_Post_Query String_Http Parameters - Fatal编程技术网

Angular 如何使用HttpParams以查询字符串参数的形式在POST请求中传递数据

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

我使用HttpParams在Angular8中将一些数据作为查询字符串参数作为POST方法传递。在检查网络选项卡时,API没有使用查询字符串参数形成,请求头也不正确。请找到下面的代码库,如果有人对此有任何意见,请告诉我

组件技术

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);
}

有人知道上述问题吗?