如何读取组件中的http状态代码错误

how to read http status code error in component

我知道之前有人问过这个问题,但我似乎找不到答案,如何从 http.. 读取状态代码?

this.bookingService.save(this.param)
    .subscribe(
        data =>
            swal({
                "title": "Succes!",
                "text": "Your data saved",
                "type": "success",
                "confirmButtonClass": "btn btn-secondary m-btn m-btn--wide"
            }),
        error => console.log('error'),
            () => this.router.navigate('Succes')
    );

您可以从错误中获取状态码,

this.bookingService.save(this.param).subscribe(
data =>{
  console.log(data);
},
(err) => {console.log(err)});

要取回错误消息,请添加一个将 return 错误对象的 catch:

 save(book){
      return this.http.post('http:..../',book)
        .map(res => res)
        .catch(this.handleError);
 }

  private handleError(error: any) { 
      let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
      return Observable.throw(error);
  }