【问题标题】:Handle error codes (401, 403) with Restangular使用 Restangular 处理错误代码(401、403)
【发布时间】:2013-12-10 02:21:07
【问题描述】:

我正在使用 Restangular 构建应用程序。我会在响应拦截器中在一个地方实现对 401、403、500 等错误的处理。

这是我写的:

RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
  switch(response.meta.code) {
    case 200:
      // Handle 200
    break;

    case 401:
      // Redirect to login
    break;

    /* ... */

    default:
    break;
  }
}

实际发生的情况是 200 被正确看到,但是对于 401,这个方法根本没有命中。 我从服务器端获得的 JSON 格式如下,以便成功响应:

{"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"response":"value"}}

{"meta":{"apiVersion":"0.1","code":401,"errors":[{"code":401,"message":"Unauthorized"}]},"response":null}

对于错误。响应标头也包含Status Code:401 Unauthorized。 我做错了什么?

【问题讨论】:

    标签: angularjs restangular


    【解决方案1】:

    401 并且任何错误代码都会命中 errorInterceptor,而不是 responseInterceptor。 responseInterceptor 仅在响应成功时调用!

    【讨论】:

    • 在 errorInterceptor 上,我得到了完整的响应,即带有标题等,并且同样返回到我的错误挂钩。有什么方法可以提取数据并仅返回?我正在尝试从 errorInterceptor 返回数据,但我仍然在我的错误挂钩中收到完整的响应。
    • 澄清一下 - 所有 4xx 或 5xx 状态码都使用 errorInterceptor,所有其他状态码都使用 responseInterceptor - 这是正确的吗?
    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2016-02-14
    • 2016-12-30
    • 1970-01-01
    • 2019-11-12
    • 2012-08-06
    相关资源
    最近更新 更多