javascript - AngularJS 启动 session

标签 javascript angularjs session cookies

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

相关文章:

javascript - 首先按数字中的给定属性对对象数组进行排序,然后是字母(数字具有子排序)

javascript - 在 wordpress 帖子中使用 ajax 提取 HTML 内容

javascript - element.all(locator).first() vs element(locator) 有警告?

javascript - AngularJS 禁用 ng-repeat 中未选中的复选框

ruby-on-rails - 使用 Rails 进行安全和不安全的 session

javascript - 闭包中的变量但无法访问

javascript - Vuejs <keep-alive> 有什么意义?

css - 使用选择器在 2 个全局 SCSS 之间切换

php - 如何在 phpspec 测试中使用 laravel session

python - 列更新时的 sqlalchemy 事件