how to read http status code error in component

纵饮孤独 提交于 2019-12-29 01:26:26

问题


I know this has been asked before but I can't seem to find the answer, how to read status code from 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')
    );

回答1:


You can get the status code from the error,

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

To get the error msg back, add a catch that will return the error object:

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


来源:https://stackoverflow.com/questions/51757895/how-to-read-http-status-code-error-in-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!