属性"responseType"的类型不兼容 [英] Types of property 'responseType' are incompatible

查看:497
本文介绍了属性"responseType"的类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在接受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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆