为什么 post 工作并且 get 抛出错误? Angular 服务
Why post works and get is throwing error? Angular service
我需要获取特定用户的总订单,
这是我的服务代码/当我使用get时/:
getTotal(userId:string): Observable<OrderTransaction[]> {
const apiUrl = environment.apiUrl + '/OrderTransaction/GetAllByUserId';
return this._http.get<OrderTransaction[]>(apiUrl, userId)
.catch(
(error: HttpErrorResponse) => {
return Observable.throw(error);
});
}
它说:
为什么会这样?
当我将 _http.get
更改为 _http.post
时,它起作用了,这是为什么?
谢谢大家
干杯
之所以有效,是因为 Angular HttpClient
中的 POST
接受作为第二个参数的正文,它可以是字符串,而 GET
请求则不是这种情况。这接受 HttpParams 类型的对象。
要获取特定数据,您可以使用 HttpParams,见下文:
let params = new HttpParams();
params = params.append('userId', userId);
return this.httpClient
.get(this.url, {params});
我需要获取特定用户的总订单,
这是我的服务代码/当我使用get时/:
getTotal(userId:string): Observable<OrderTransaction[]> {
const apiUrl = environment.apiUrl + '/OrderTransaction/GetAllByUserId';
return this._http.get<OrderTransaction[]>(apiUrl, userId)
.catch(
(error: HttpErrorResponse) => {
return Observable.throw(error);
});
}
它说:
为什么会这样?
当我将 _http.get
更改为 _http.post
时,它起作用了,这是为什么?
谢谢大家 干杯
之所以有效,是因为 Angular HttpClient
中的 POST
接受作为第二个参数的正文,它可以是字符串,而 GET
请求则不是这种情况。这接受 HttpParams 类型的对象。
要获取特定数据,您可以使用 HttpParams,见下文:
let params = new HttpParams();
params = params.append('userId', userId);
return this.httpClient
.get(this.url, {params});