cordova 401(未授权)获取方法时出错

goucqfw6  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(156)

我正在使用HTTP Auth Interceptor Module创建一个简单的登录应用程序。
在我LoginController中,我有:

angular.module('Authentication')
.controller('LoginController',
['$scope', '$rootScope', '$location', 'AuthenticationService',
function ($scope, $rootScope, $location, AuthenticationService) {
    // reset login status
    AuthenticationService.ClearCredentials();

    $scope.login = function () {
        $scope.dataLoading = true;
        AuthenticationService.Login($scope.username, $scope.password, function (response) {
            if (response.success) {
                AuthenticationService.SetCredentials($scope.username, $scope.password);
                $location.path('/');
            } else {
                $scope.error = response.message;
                $scope.dataLoading = false;
            }
        });

    };
}]);

以下是对它的简单服务:

angular.module('Authentication')

.factory('AuthenticationService',
['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
function (Base64, $http, $cookieStore, $rootScope, $timeout, $scope) {
    var service = {};

    service.Login = function ($scope, username, password, callback) {

        $http
            .get('http://Foo.com/api/Login', 
                        { username: username, password: password } , {withCredentials: true}).
                        then(function (response) {
                                console.log('logged in successfully');
                                callback(response);
                        }, function (error) {
                                console.log('Username or password is incorrect');
                        });
    };

    service.SetCredentials = function (username, password) {
        var authdata = Base64.encode(username + ':' + password);

        $rootScope.globals = {
            currentUser: {
                username: username,
                authdata: authdata
            }
        };

        $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; 
        $http.defaults.headers.common['Content-Type'] = 'application/json'
        $cookieStore.put('globals', $rootScope.globals);
    };

    service.ClearCredentials = function () {
        $rootScope.globals = {};
        $cookieStore.remove('globals');
        $http.defaults.headers.common.Authorization = 'Basic ';
    };

    return service;
}])

这是我的登录页面:

当我尝试在浏览器中测试时,没有成功登录,甚至没有收到错误消息,而是出现了这样的弹出窗口:

我不明白的是为什么从登录表单传递的凭据没有被考虑在内。以及我如何才能摆脱这个弹出窗口。当我取消这个弹出窗口时,同样不是得到http请求的错误,而是在控制台中得到401(未授权)错误。我错过了什么?
我还在Emulator中运行,而不是得到任何错误,应用程序停留在加载部分。

6qfn3psc

6qfn3psc1#

1.将您的URL更改为类似以下内容

.get('https://username:password@Foo.com/api/Login',...

1.使用以下示例处理401错误:

.config(['$httpProvider', function($httpProvider) {
     $httpProvider.defaults.useXDomain = true;
     $httpProvider.defaults.withCredentials = true;
     $httpProvider.interceptors.push(['$q', function ($q) {
       return {
         'responseError': function (rejection) {
           if (rejection.status === 401) {
             console.log('Got a 401');
           }
           return $q.reject(rejection)
         }
       }
     }]) 
 }])
ruarlubt

ruarlubt2#

下面是工作代码片段。从这里复制粘贴即可。

routerApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    $httpProvider.interceptors.push(['$q', function ($q) {
      return {
        'responseError': function (rejection) {
          if (rejection.status === 401) {
            window.console.log('Got a 401');
          }
          return $q.reject(rejection);
        }
      };
      }]);
}]);

相关问题