gpt4 book ai didi

javascript - AngularJS 启动 session

转载 作者:行者123 更新时间:2023-12-03 09:17:48 25 4
gpt4 key购买 nike

我想在确认登录后开始一个新 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com