我正在使用 json-server 并希望將資料作為帶有請求正文的 post 方法請求。它適用于 Postman,但不適用于 Angular。在 Angular 中,無論我嘗試什么,內容型別總是文本/純文本。我錯過了什么?
getProjects(request: ProjectRequest): Observable<Project[]> {
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', localStorage.getItem('accessToken')!);
const body = JSON.stringify(request);
return this.http.post<Project[]>(this.apiGetProjects, body, { headers: headers });
}

uj5u.com熱心網友回復:
你不必將身體串起來。就這樣做
this.http.post<Project[]>(this.apiGetProjects, request, { headers: headers });
無關
- 不要設定
content-type標題。Angular 會為你做這件事。 - 不要在
authorization這里設定標題。只需使用HttpInterceptor. 看看這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525326.html
標籤:有角度的休息http
