catch api 404 response with Angular2 http

2021-6-21 anglehua

I have api which returns 400 or 404 errors if failed.

When I run the http.get method, it does not catch the error on subscribe and throws error.

Http.get(`api/path/here`).map(res => res.json()).subscribe(
            data => console.log(data),
            error => console.log(error)
        );

Following is the error I get

enter image description here


.subscribe(res=>{
    this.data=res;
    console.log('bye');
 },
 (err)=>console.log(err),
 ()=>console.log("Done")
 );

Try out this way.



You could also use the catch operator

http.get(`api/path/here`)
    .map(res => res.json())
    .catch(
        err => {
          // handle errors
        })
    .subscribe(
        data => console.log(data)
    );


采集自互联网,如有侵权请联系本人

Powered by emlog 京ICP备15036472号-3 sitemap