代码之家  ›  专栏  ›  技术社区  ›  chris01

RxJS HTTP Client:not JSON的响应类型

  •  0
  • chris01  · 技术社区  · 6 年前

    我不知道这里有什么问题。我想发出一个HTTP请求,它确实响应二进制数据(例如文件下载),而不是JSON。

    import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
    

    let x = this.http.get <any> ("http://myhost", { responseType : 'json' });
    let y = this.http.get <any> ("http://myhost");
    

    但我需要将响应定义为非JSON。

    let z = this.http.get <any> ("http://myhost", { responseType : 'string' });
    

    这里我有个问题。我也试过“blob”。 我总是在编译时得到一个typescript错误。

    属性“responseType”的类型不兼容。 类型“string”不能分配给类型“json”。

    1 回复  |  直到 6 年前
        1
  •  2
  •   user184994    6 年前

    如果数据是文件,请使用 blob ,但您需要删除泛型参数。使用:

    his.http.get("http://myhost", { responseType : 'blob' });
    

    推荐文章