how to read http status code error in component

后端 未结 1 1031
孤城傲影
孤城傲影 2021-01-14 18:53

I know this has been asked before but I can\'t seem to find the answer, how to read status code from http..?

相关标签:
1条回答
  • 2021-01-14 19:27

    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);
      }
    
    0 讨论(0)
提交回复
热议问题