2016-08-31 47 views
1

我使用deployd模擬http請求。我想用我的憑據對數據庫進行身份驗證。http呼叫總是成功,無論憑據

這是我的控制器代碼

.controller("authCtrl", function($scope, $http, $location, authUrl) { 


$scope.authenticate = function (user, pass) { 
    $http.post(authUrl, { 
     username: user, 
     password: pass 
    }).success(function (data) { 
     console.log(data); 
     $location.path("main"); 
    }).error(function (error) { 
     console.log("error"); 
     $scope.authenticationError = error; 
    }); 
} 

})

我得到輸入憑據錯誤之一後,下面的消息。

Object { message: "bad credentials", status: 401 } 

我在正確輸入兩個憑據後得到以下消息。

Object { path: "/users", id: "11a58ccdcc31c9b863b7a5e63d9d672fdae…", uid: "e2ad770f0847f8ac" } 

我的問題是沒有得到「錯誤」日誌。只有成功事件被解僱。輸入憑據錯誤或權利不會改變這種情況。

+0

您運行的是什麼版本的角度? – cbass

+0

你有沒有改變'responseError'處理程序像[這裏](http://stackoverflow.com/questions/15888162/angularjs-http-error-function-never-called)? –

+0

我正在使用AngularJS v1.2.0。 –

回答

0

嘗試追趕error.status = 401,例如從https://github.com/moorthi07/MarsCMS/

$scope.login = function() { 
      $scope.newUser.$login().then(function (success) { 
       $rootScope.$broadcast('user:login'); 
       $state.go('dashboard.intro'); 
      }, function (err) { 
       if (err.status == 401) { 

    //    $mdToast.show(
    //     $mdToast.simple() 
    //     .content('Username or password invalid!') 
    //     .position('right bottom') 
    //    ); 
       } else { 

    //    $mdToast.show(
    //     $mdToast.simple() 
    //     .content('An error occured. Please check log.') 
    //     .position('right bottom') 
    //    ); 
        console.log(err); 
       }; 


      }); 
     };