Angular 12 HttpClient POST 到字符串无法编译

usr-local-ΕΨΗΕΛΩΝ

我有一个POST方法返回一个纯字符串。

以下不编译

public ping(): Observable<string> {
    return this.httpClient.post<string>(this.uri, {}, {
      observe: 'body', responseType: 'text'
    });
  }

错误是

TS2322: Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<string>'.     Type 'ArrayBuffer' is not assignable to type 'string'

看来Webstorm坚持要把我的语句编译成下面的HttpClient(ctrl-clicked)的公共方法

post(url: string, body: any | null, options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
    context?: HttpContext;
    observe?: 'body';
    params?: HttpParams | {
        [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
    };
    reportProgress?: boolean;
    responseType: 'arraybuffer';
    withCredentials?: boolean;
}): Observable<ArrayBuffer>;

我如何正确地告诉 Angular 服务器正在返回一个普通的string

琼斯夏普

这是通用类型令人困惑的事情,以下工作正常:

return this.httpClient.post(this.uri, {}, {
  observe: 'body', responseType: 'text'
});

如果查看实现,您只能看到三个带有泛型类型参数 的重载post<T>,所有这些都为responseType: 'json'鉴于编译器无法匹配任何重载,它会向您显示应用为该方法列出第一个重载的错误,该方法确实返回Observable<ArrayBuffer>.

请注意,对于您实际想要的重载,该observe参数是可选的,因此您可以执行以下操作:

return this.httpClient.post(this.uri, {}, { responseType: 'text' });

此外,当我实际尝试使用损坏的服务进行构建时,我遇到了一个比在 IDE 中内联更有用的错误;它实际上告诉我没有应用任何重载:

ERROR in src/app/service.ts:11:45 - error TS2769: No overload matches this call.
  Overload 1 of 15, '(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
    Type '"text"' is not assignable to type '"json"'.
  Overload 2 of 15, '(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
    Type '"text"' is not assignable to type '"json"'.
  Overload 3 of 15, '(url: string, body: any, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
    Type '"text"' is not assignable to type '"json"'.

11     return this.http.post<string>("", {}, { responseType: "text" });
                                               ~~~~~~~~~~~~

  node_modules/@angular/common/http/http.d.ts:2297:9
    2297         responseType?: 'json';
                 ~~~~~~~~~~~~
    The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
  node_modules/@angular/common/http/http.d.ts:93m2413:9
    2413         responseType?: 'json';
                 ~~~~~~~~~~~~
    The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
  node_modules/@angular/common/http/http.d.ts:2458:9
    2458         responseType?: 'json';
                 ~~~~~~~~~~~~
    The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular 5 HttpClient:将POST参数作为URL编码的字符串发送

Angular HttpClient发布无法发送简单的字符串参数

如何通过 Angular 的 HttpClient 发送 POST 请求?

Angular HttpClient在POST中使用URL参数

带有凭证的Angular 6 httpClient Post

Angular HTTPClient POST主体未发送

Angular HttpClient:如果get()失败,则执行post()

Angular HttpClient无法代理

Angular 4/5 HttpClient:字符串类型的参数无法分配给“ body”

angular4:无法使用httpclient get获得响应字符串

Angular 8-可能无法与Socket.io结合使用httpClient POST来完成其工作?

使用HttpClient在Angular中测试文件上传。无法获取HTTP POST正文

Angular httpClient.post() 无法像 Postman 那样获取 API 返回的错误对象

如何让Angular的HttpClient返回对象而不是字符串?

Angular HttpClient - 仅错误响应字符串

Angular HttpClient对象到数组

Angular-将错误类型定义为HttpClient.post()

Angular 5 Service httpClient Post - 标题未定义

为什么这个 Angular HttpClient Post 没有同步?

Angular HttpClient不发送POST,它发送OPTIONS

Angular HttpClient:Post方法未正确设置参数

Angular httpClient - POST 成功,没有发送参数

来自HttpClient POST请求的Angular 6 HTML响应

无法将响应从httpclient post函数返回到Angular 4中的其他自定义函数

Angular 12 中的 AOT 编译

commons httpclient-将查询字符串参数添加到GET / POST请求

.NET HttpClient将查询字符串和JSON正文添加到POST

使用HttpClient通过HTTP POST Windows Phone 8.1上传图像和字符串

Typescript HttpClient.post 从 json 格式的字符串返回错误的请求