【发布时间】:2021-05-13 22:16:10
【问题描述】:
我无法在 Angular 5 中发出接受 text/plain 作为响应的 POST 请求。 Angular 正在调用一个期望 JSON 作为响应的方法,因此在尝试解析响应时出现响应时会引发错误。
我尝试使用参数 {responseType: 'text'} 调用方法,但 VS 代码将其显示为错误,并且在编译应用程序时我也在控制台中收到错误。
下面是 Angular 5 的 POST 请求代码,它期望响应为 text/plain。
this.http
.post<string>(this.loginUrl, this.user, {responseType: 'text'}) // error in this line
.subscribe(
(data) => this.success(data),
(error) => this.failure(error)
);
编译时控制台显示以下错误:
ERROR in src/app/login/login.component.ts(47,47): error TS2345: Argument of type '{ responseType: "text"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Types of property 'responseType' are incompatible.
Type '"text"' is not assignable to type '"json"'.
【问题讨论】:
标签: angular typescript