gpt4 book ai didi

angularjs - 如何跳过 HTTP 请求的 angularjs 拦截器?

转载 作者:行者123 更新时间:2023-12-03 15:08:44 26 4
gpt4 key购买 nike

我有一个 angularjs 应用程序,其中我有一个拦截器,可以将授权 token 添加到每个请求的 header 中。

但是,我需要在应用程序中的某个地方使用拦截器破坏它的外部 API,因为它添加了此外部 API 提供者 Not Acceptable 授权 header 。如何让 angularjs HTTP 跳过拦截器,仅在这种特定情况下?

拦截器代码如下:

app.factory('authInterceptorService', ['$q', '$injector', '$location', 'localStorageService', function ($q, $injector, $location, localStorageService) {
var authInterceptorServiceFactory = {};
var $http;

var _request = function (config) {

config.headers = config.headers || {};

var authData = localStorageService.get('authorizationData');
if (authData) {
//console.log("token: " + authData.token.substring(0, 10));
//console.log("user: " + authData.userName);
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
}

var _responseError = function (rejection) {
var deferred = $q.defer();
if (rejection.status === 401) {
var authService = $injector.get('authService');
authService.refreshToken().then(function (response) {
_retryHttpRequest(rejection.config, deferred);
}, function () {
authService.logOut();
$location.path('/login');
deferred.reject(rejection);
});
} else {
deferred.reject(rejection);
}
return deferred.promise;
}

var _retryHttpRequest = function (config, deferred) {
console.log('retrying');
$http = $http || $injector.get('$http');
$http(config).then(function (response) {
deferred.resolve(response);
//console.log("success:" +response);
}, function (response) {
deferred.reject(response);
//console.log("error:" + response);
});
}

authInterceptorServiceFactory.request = _request;
authInterceptorServiceFactory.responseError = _responseError;

return authInterceptorServiceFactory;
}]);

最佳答案

简单

$http.get("url" , {noAuth : true}).then(success(),error());

在拦截器中
  var _request = function (config) {

config.headers = config.headers || {};

var authData = localStorageService.get('authorizationData');
if (authData && !config.noAuth) {
//console.log("token: " + authData.token.substring(0, 10));
//console.log("user: " + authData.userName);
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
}

关于angularjs - 如何跳过 HTTP 请求的 angularjs 拦截器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com