无法读取localhost cookie(返回undefined)

时间:2015-10-04 20:41:09

标签: angularjs cookies angular-cookies

尝试使用ngCookies读取cookie。 有一个cookie:

Name: cookie-name
Content:value
Host:localhost

controller.js:

var myControllers = angular.module('myControllers', ['ngCookies']);

myControllers.controller('AboutController', [
'$scope', 
'$location',
'$cookies', 
function ($scope, $location, $cookies) {
    $scope.test1 = $cookies.get('cookie-name');
    $scope.test2 = '2';
}]);

html是:

<div class="jumbotron text-center">
    <h1>About Page</h1>
    <p>test1:{{ test1 }}</p>
    <p>test2:{{ test2 }}</p>
</div>

但结果页面是:

About Page

test1:

test2:2

更新

我在spring controller中创建了cookie。

import javax.servlet.http.Cookie;
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public AuthenticationResponse logIn(
        @RequestParam(value="username") String username,
        @RequestParam(value="password") String password,
        HttpServletResponse response) 
    {
        /* Test setting cookie */
        Cookie cookie = new Cookie("cookie-name", username);
        cookie.setMaxAge(3600*10);
        response.addCookie(cookie);
        AccessToken token = userService.authenticateUser(username, password);
        return new AuthenticationResponse(token.getEncodedToken(), token.getExpiryDate());
    }

我不知道为什么无法成功读取cookie。任何建议都是受欢迎的。感谢。

0 个答案:

没有答案