属性"responseType"的类型不兼容 [英] Types of property 'responseType' are incompatible
问题描述
我无法在接受text/plain
作为响应的Angular 5中发出POST请求. Angular正在调用一个期望JSON
作为响应的方法,因此在尝试解析响应时响应出现时会引发错误.
I am unable to make a POST request in Angular 5 that accepts text/plain
as response. Angular is calling a method which expects JSON
as response and hence throws an error when response comes while trying to parse the response.
我尝试使用参数{responseType: 'text'}
调用方法,但是VS代码将其显示为错误,并且在编译应用程序时控制台中也出现错误.
I tried calling method with parameter {responseType: 'text'}
but VS code is showing it as an error and I am also getting an error in the console when the application is compiled.
下面是POST请求的Angular 5代码,期望该响应为text/plain
.
Below is the Angular 5 code for a POST request that expects a response as 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)
);
在编译时,控制台中显示以下错误:
While compilation the following error show in the console:
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"'.
推荐答案
接受结果responseType:'text'
的post
版本不是通用的,因为结果类型已经明确(即string
)
The version of post
accepting responseType:'text'
is not generic, since the result type is already clear (ie string
)
this.http
.post(this.loginUrl, this.user, {responseType: 'text'}) // no generic parameter
.subscribe(
(data) => this.success(data), // data is string
(error) => this.failure(error)
);
这篇关于属性"responseType"的类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!