AngularJS成功401拦截还是抛出401

AngularJS Successful 401 Intercept Still Throws 401

我有一个有效的 angular 拦截器服务,它在从服务调用收到 401 响应时重定向到登录页面

问题是 401 在 Google Chrome 控制台

中仍然显示为 JavaScript 错误
GET http://domain:port/api/url 401 (Unauthorized) ... angular.js:9658

这是 chrome 的功能,还是我需要以其他方式处理?

我的working拦截器服务内容如下

var _request = function (config) {

    /*insert auth headers*/
}

var _responseError = function (rejection) {

    if (rejection.status === 401) {

        $location.path('/login');
    }

    return $q.reject(rejection);
}

var _response = function (response) {
    return response || $q.when(response);
}

提前感谢您的帮助

看来你误解了[Angular拦截器]的机制。

For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses, it is desirable to be able to intercept requests before they are handed to the server and responses before they are handed over to the application code that initiated these requests.

拦截器的目的是在将响应移交给应用程序代码之前对其进行预处理,这意味着它介于浏览器和您的个人代码之间。由于服务器响应401错误,浏览器肯定会认为是401 unauthorized,标记为红色并写入日志到console面板。

然后 Angular 拦截器从浏览器接收 http 响应数据并按照您的要求对其进行预处理。完成所有这些后,您的 $http 调用将收到最终响应对象。

是的,这是浏览器的一项功能。如果您认为错误消息很烦人并且您不希望用户看到它,您可以将您的响应代码重写为其他代码(例如 0、600,任何不与所有 HTTP 响应代码冲突的代码),然后在您的拦截器中处理它们.这是一个技巧。