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

时间:2019-03-19 17:47:21

标签: angular angular-httpclient

如何获取服务代码?我尝试使用下面的代码,但没有记录任何内容。

在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);
      })
    }

3 个答案:

答案 0 :(得分: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 };
    });

答案 1 :(得分:0)

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

sendForgotPass() {
    return this.service.postForgotPass(this.emailFormControl.value)
        .subscribe(res => console.log(res.status), err => console.log('error', err.status))
    }

答案 2 :(得分:0)

if(response === undefined || response === ''  || response === null)
{  
    this.isStatusCode[i] = false;
}