【问题标题】:HttpClient from @angular/common/http来自 @angular/common/http 的 HttpClient
【发布时间】:2017-09-26 16:13:52
【问题描述】:

我正在使用 HttpClient 发送请求,但无法解析响应。 使用来自 @angular/http 的 http 调用 response.json().token 不是问题,但这在 HttpClient 上不存在。

这是我的要求:

 return this.httpClient.post(environment.baseUrl + '/api/authenticate', JSON.stringify({ username: username, password: password }))
            .map((response: Response) => {

//response.json() --> leads to an exception
...

回复是: {response = {"token": "abcd...xyz"}}

这个不能用HttpClient吗?

【问题讨论】:

标签: angular


【解决方案1】:

与旧的Http 服务不同,新的HttpClient 服务会尝试猜测内容类型并进行相应的解析,因此您无需调用response.json(),因为response 已经是一个对象。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2017-12-25
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多