以下是我使用的代码,如果基本身份验证成功,我会收到身份验证成功警报,但是当凭据错误时,永远不会显示 else 警报“身份验证失败”。我不使用任何路线,也不需要使用拦截器。有没有办法在不使用拦截器的情况下获得 401 错误?
this.authorize = function(request, callbackFunc) {
var encodedString = btoa(request.userName + ":" + request.password);
var basicAuthString = 'Basic ' + encodedString;
var requestObject = {
location: '40005'
};
var req = {
method: this.method,
crossDomain: true,
url: this.loginURL,
data: requestObject,
headers: {
'Authorization': basicAuthString,
'Content-Type': 'application/json',
'Accept': '*/*',
'apiKey': ''
}
};
$http(req)
.success(function(response) {
callbackFunc(response, "success");
})
.error(function(response) {
console.log("Error Received");
callbackFunc(response, "error");
});
};
在控制器中:
$scope.Login = function() {
AuthenticationService.authorize($scope.LoginRequest, function(response, responseCode) {
if (responseCode === "success") {
alert("Authentication Success");
} else {
alert("Authentication Failed");
}
});
};