1

我有一个登录系统,可以将会话保持 2 小时。2 小时后,当客户端调用 API 时,返回错误:

{
  "Success": false,
  "Error": {
    "ErrorCode": "002",
    "ErrorMessage": "Session expired"
  }
}

然后我需要将客户端重定向到/login发生这种情况的时间。问题是我从单个 API 调用中得到了那个错误,所以不是改变每个 API 调用,是否可以有一个全局拦截器?

4

1 回答 1

2

按照Interceptorsfound here的示例,您可以response在拦截器部分执行以下操作:

response: function(resp){
    // resp.data will contain your response object from the server
    if(resp.data && !resp.data.Success){
        // check for error code
        if(resp.data.Error && resp.data.ErrorCode === '002'){
            $location.path('/login');
            return $q.reject(resp);
        }
    }
    return resp || $q.when(resp);
},

这是假设您的服务器200在收到此错误消息时返回 a ,而不是在 a 401or上403,但这就是您正在做的事情。希望有帮助。

于 2013-10-21T14:31:35.230 回答