如何在 angularjs 中的 ajax 请求后重定向?
How do I redirect after ajax request in angularjs?
我正在为我的项目使用 angularjs 和 nodejs。现在,在我使用后台调用进行身份验证之后。现在,在我收到成功的身份验证后,如何将用户重定向到仪表板?这是我的登录名 div:
<div ng-controller="loginCtrl" class="control">
<form role="form" name="docRegForm" ng-submit="login()" enctype="multipart/form-data" class="col-xs-11 div-center">
<div class="input-group">
<input id="exampleInputEmail1" type="text" placeholder="Username" ng-model="user.username" class="form-control"/>
</div>
<div class="input-group">
<input id="exampleInputPassword1" type="password" placeholder="Password" ng-model="user.password" class="form-control"/>
</div>
<div class="col-xs-12 div-center">
<button type="submit" class="btn btn-danger full-width">LOGIN</button>
我的 angular 控制器是:
app.controller('loginCtrl', function ($scope, $http, $window) {
$scope.message = '';
$scope.login = function () {
$http
.post('/authenticate', $scope.user)
.success(function (data, status, headers, config) {
$window.localStorage.nimbusToken = data.token;
console.log($window.localStorage.token);
$scope.message = 'Welcome';
};
alerts[data.status];
})
.error(function (data, status, headers, config) {
// Erase the token if the user fails to log in
alert("failure");
delete $window.localStorage.token;
// Handle login errors here
$scope.message = 'Error: Invalid user or password';
});
};
});
现在登录后,如果登录失败,我必须重定向到仪表板或重新登录。我该怎么做?
成功使用:-
$location.path("/仪表板");
错误使用:-
$location.path("/登录");
如果 $location.path('')
不适合你,试试这些:
// similar behavior as an HTTP redirect
window.location.replace("your path.");
或:
window.location.replace("your path.")
我正在为我的项目使用 angularjs 和 nodejs。现在,在我使用后台调用进行身份验证之后。现在,在我收到成功的身份验证后,如何将用户重定向到仪表板?这是我的登录名 div:
<div ng-controller="loginCtrl" class="control">
<form role="form" name="docRegForm" ng-submit="login()" enctype="multipart/form-data" class="col-xs-11 div-center">
<div class="input-group">
<input id="exampleInputEmail1" type="text" placeholder="Username" ng-model="user.username" class="form-control"/>
</div>
<div class="input-group">
<input id="exampleInputPassword1" type="password" placeholder="Password" ng-model="user.password" class="form-control"/>
</div>
<div class="col-xs-12 div-center">
<button type="submit" class="btn btn-danger full-width">LOGIN</button>
我的 angular 控制器是:
app.controller('loginCtrl', function ($scope, $http, $window) {
$scope.message = '';
$scope.login = function () {
$http
.post('/authenticate', $scope.user)
.success(function (data, status, headers, config) {
$window.localStorage.nimbusToken = data.token;
console.log($window.localStorage.token);
$scope.message = 'Welcome';
};
alerts[data.status];
})
.error(function (data, status, headers, config) {
// Erase the token if the user fails to log in
alert("failure");
delete $window.localStorage.token;
// Handle login errors here
$scope.message = 'Error: Invalid user or password';
});
};
});
现在登录后,如果登录失败,我必须重定向到仪表板或重新登录。我该怎么做?
成功使用:-
$location.path("/仪表板");
错误使用:-
$location.path("/登录");
如果 $location.path('')
不适合你,试试这些:
// similar behavior as an HTTP redirect
window.location.replace("your path.");
或:
window.location.replace("your path.")