http - 使用 Angular2 http 捕获 api 404 响应

标签 http http-status-code-404 response angular

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

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

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

以下是我得到的错误

enter image description here

最佳答案

你也可以使用catch操作符

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

关于http - 使用 Angular2 http 捕获 api 404 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519483/

相关文章:

html - 在 OCaml 网络爬虫中跟踪 HTTP 重定向

google-app-engine - 静态网站的appengine自定义404页面

spring - HTTP 状态 404 -/gwtspring/com.javacodegeeks.gwtspring.Application/Application.html

c# - WCF 客户端如何使用多部分/相关的 Java Web 服务响应?

web-services - 使用 Perl 将文件上传到外部服务器

perl 在 HTTP::Request 中支持 IPv6 地址格式 [::1]

java - Spring Boot 应用程序 - 在 tomcat 上找不到 404

javascript - 在 Datalist 问题中填充数据

JavaServlet : pass a request back to default processing

windows - 尝试在 Windows 7 上的 Strawberry Perl 上安装 HTTP::Server::Brick