【问题标题】:Angular 6 - HTTP Response 200 is null in subscribe [duplicate]Angular 6 - HTTP Response 200 在订阅中为空 [重复]
【发布时间】:2018-06-10 17:55:12
【问题描述】:

我有以下 Angular http.service.ts 用于调用 /login API。

login(user: User) {
    console.log("logging in");
    console.log(JSON.stringify(user));
    this.http
        .post<any>(this.loginURL, user, httpOptions)
        .subscribe(
            res => console.log('response : ' + res),
            err => console.log('error : ' + err))
}

这是一个非常标准的POST 调用,但它总是返回null,我不明白为什么。 这是Network谷歌浏览器的标签信息

Chrome 的Console 信息

我不明白为什么回复是null。即使我没有有效载荷,它至少应该使headers 不可用?

【问题讨论】:

  • 在 chrome 开发工具中打开您的网络选项卡并检查帖子网址的响应,我猜角度与响应无关,而是在控制台中显示它。 “检查你的后端”。
  • 其实我在贴的照片中没有看到回复。

标签: angular post angular6 angular-httpclient


【解决方案1】:

尝试以下方法:

this.http.post("url", body, {headers: headers, observe: "response"})
   .subscribe(res => console.log(res));

您可以使用与您相同的 http 选项,但添加观察:response。标头是来自 http 客户端的 HttpHeaders。例如:headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),

【讨论】:

  • 您可以采用与您相同的 http 选项,但添加 observe: "response"。标头是来自 http 客户端的 HttpHeaders。例如:headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),
  • 它有效!!!,虽然现在 response.headers 是一个空数组,即使在网络谷歌浏览器的标签信息中我看到了很多:-(
【解决方案2】:

试试下面的,

this.http.post(loginUrl,user,httpOptions).map( res =>res).subscribe(data => {
  console.log(data);
});

【讨论】:

  • 您好,我使用了以下方法,但仍然无法正常工作。我的回复还是null.post(this.loginURL, user, httpOptions) .pipe(map(res =&gt; res)) .subscribe( res =&gt; console.log('response : ' + res)
  • 我需要显示使用“数据”的消息我如何在组件中访问“数据”?
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 2022-01-05
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 2020-10-08
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多