javascript - Angularjs(1.3.6+) $resource 拦截器和全局 401 处理程序

标签 javascript angularjs http-status-code-401 angular-http-interceptors

我的问题是关于$resource 的拦截器(responseError)。我想强调一下,我基于的angularjs是V1.3.6

问题:

app.factory('authInterceptor',['$q', '$location', '$log', function($q, $location, $log){
    $log.debug('this is a regular factory with injection');
    return {
        responseError: function(response){
            console.log(response)
            // problem is that I cant check 401 by response.status, 
            // because the response here is AN ERROR OBJECT like `SyntaxError: ...`. Anyway to get the status?
            return $q.reject(response);
        }
    }
}])

当我收到 401 响应时,responseError 的参数是一个错误对象,如 SyntaxError: Unexpected token U 因为来自服务器的响应是纯文本 Unathorized 状态为 401

但我想获取 response.status,如果是 401 则执行一些操作。

我们将不胜感激。

最佳答案

这个问题应该关闭,因为我终于找到了答案。

当响应为 401/404 和 200 以外的任何内容时,transformResponse 仍会执行并且发生错误!这个错误只是覆盖了正常的响应(有状态属性),所以我从来没有在拦截器中得到原始响应!

如果响应的状态不是 200,我认为执行 transformResponse 是愚蠢的!在 transformResponse 中,您无法访问状态代码...

关于javascript - Angularjs(1.3.6+) $resource 拦截器和全局 401 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27772740/

相关文章:

javascript - 如何在没有指令的情况下在元素上创建隔离范围?

JAVA JPA错误401 : "Full authentication is required to access this resource"

javascript - 为什么从 onSubmit 传递的 evt 未定义?

javascript - Adobe DPS Webviewer getByProductId 不起作用

javascript - 如何将灰度应用于图像并添加文本

javascript - 使用 Jquery 函数删除 css 类的所有实例

javascript - 子状态更改时 Angular 重新加载父 Controller

angularjs - 在 Angular 初始化结束时执行代码,并显示ngCloak

python - 使用 http 请求更新谷歌电子表格

authentication - Asp.Net Core 2 oidc中间件在代理请求返回401后不进行质询