【问题标题】:$cookies is lost after page refreshing页面刷新后 $cookies 丢失
【发布时间】:2016-01-26 22:55:19
【问题描述】:

我尝试将登录信息保存在 cookie 中。在我的方法之后,cookies 注册了良好的数据。但是,当我重定向到下一页时,cookie 会丢失。我不明白为什么。如果有人可以解释一下。

这是我的登录方式:

 $scope.Login = function()
    {
        var expireDateCookies = new Date();

        expireDateCookies.setHours(expireDateCookies.getHours() + 6);


        // default post header to url encoded content
        $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

        //variable who contains the authentication information
        var auth = $scope.auth;

        var requestGetToken = $http({ method: 'POST', url: url, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: $.param({ username: auth.Login, password: auth.Password, grant_type: 'password', client_id: client_id }) });
        requestGetToken.success(function (data) {
            $cookieStore.put('access_token', data.access_token, { 'expires': expireDateCookies });
        });
        // default post header to json
        $http.defaults.headers.post['Content-Type'] = 'application/json';
        $window.location.href = '/CustomersList';
    }

感谢您给予我的所有帮助。

【问题讨论】:

标签: angularjs cookies refresh ajax-request


【解决方案1】:

您好,只是想让您知道,$cookieStore 现在已被弃用。请查看angular documentation for the same

尝试改用$cookies。这可能是您面临 cookie 清除问题的原因。

只需添加到此信息,您还可以选择 localStorage 一个 HTML 5 功能,它允许您将数据作为键值对存储在浏览器本身中。它没有有效期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2020-11-19
    • 2014-07-20
    • 1970-01-01
    • 2017-05-24
    • 2017-01-04
    • 2019-05-16
    • 2022-09-23
    相关资源
    最近更新 更多