我想在确认登录后开始一个新 session 。但是我得到了未定义的cookie,并且无法完成。这是我的代码:
<script>
var app = angular.module('myApp', ['ngCookies'])
app.controller('loginController', function($scope, $http, $cookieStore) {
$scope.login = function() {
$http.post("api/Login/" + $scope.email + "/"+ $scope.password).success(function(response) {
$scope.name = response;
var userCookie = $cookies.get('newCookie');
$cookies.put('newCookie', $scope.name);
});
};
});
</script>
如果有任何帮助,我将不胜感激。
最佳答案
cookie 尚未设置,因此您得到了未定义的 cookie。 您需要先设置 cookie,然后再检索它。因此更改代码顺序:
<script>
angular.module('myApp', ['ngCookies'])
.controller('loginController', loginController);
function loginController($scope, $http, $cookies) {
$scope.login = function() {
$http.post("api/Login/" + $scope.email + "/"+ $scope.password).success(function(response) {
$scope.name = response;
$cookies.put('newCookie', $scope.name);
var userCookie = $cookies.get('newCookie');
});
};
}
</script>
关于javascript - AngularJS 启动 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31916265/