如何读取组件中的http状态码错误 [英] how to read http status code error in component

查看:32
本文介绍了如何读取组件中的http状态码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于如何读取组件中的http状态码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆