Angular 6:如何获取响应状态代码

问题描述 投票:1回答:2

我如何获得服务代码?我尝试下面的代码,但它没有记录任何东西。

在service.ts文件中。

constructor(private http: HttpClient) { }

postForgotPass(email): Observable<Response> {

return this.http.post<Response>(envi.apiUrl +'/user/forgotpassword', {
  "email": email,
  "headers": headers,
  observe: 'response'

})
}

在我的component.ts文件中

sendForgotPass() {
 return this.service.postForgotPass(this.emailFormControl.value)
    .subscribe(
      res => {
        console.log(res.status);
      })
    }
angular angular-httpclient
2个回答
0
投票

来自Angular docs

getConfigResponse(): Observable<HttpResponse<Config>> {
  return this.http.get<Config>(
    this.configUrl, { observe: 'response' });
}

用法

this.configService.getConfigResponse()
    // resp is of type `HttpResponse<Config>`
    .subscribe(resp => {
      // display its headers
      const keys = resp.headers.keys();
      this.headers = keys.map(key =>
        `${key}: ${resp.headers.get(key)}`);

      // access the body directly, which is typed as `Config`.
      this.config = { ... resp.body };
    });

0
投票

这是因为非2xx状态代码映射到错误。你需要为订阅的第二个参数(错误)提供一个处理程序

sendForgotPass() {
    return this.service.postForgotPass(this.emailFormControl.value)
        .subscribe(res => console.log(res.status), err => console.log('error', err.status))
    }
© www.soinside.com 2019 - 2024. All rights reserved.