用Angular2 http捕获api 404响应 [英] catch api 404 response with Angular2 http

查看:142
本文介绍了用Angular2 http捕获api 404响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有api,如果失败则返回400或404错误。

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

当我运行http.get方法时,它不会捕获订阅错误并抛出错误。

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

以下是我得到的错误

推荐答案

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

试试这种方式。

这篇关于用Angular2 http捕获api 404响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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