Chrome浏览器:未从响应标头设置Cookie

时间:2015-02-25 07:11:33

标签: javascript google-chrome cookies

在服务器端创建cookie,然后添加到响应:

    Cookie cookie = new Cookie("foo", "bar");
    cookie.setPath("/abc");
    cookie.setDomain(request.getServerName());
    cookie.setSecure(request.isSecure());
    cookie.setMaxAge(24 * 60 * 60 * 90); //  90 days
    response.addCookie(cookie);
从浏览器开发人员工具

看到的

响应标头:(不是ajax)

Content-Type:text/html;charset=UTF-8
Date:Wed, 25 Feb 2015 07:13:12 GMT
Server:Apache-Coyote/1.1
Set-Cookie:foo="bar"; Version=1; Domain=localhost; Max-Age=7776000; Expires=Tue, 26-May-2015 07:13:12 GMT; Path=/abc

但是从开发工具>资源>饼干,饼干没有显示出来。

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

Even i have written the same code but the cookie was not shown up.

When i print my response i will get the value, but i can not see on my chrome browser.

Server side code :
Cookie lastConnetionDateCookie = createCookie("lastConnetionDate", lastLoginDate);
            response.addCookie(lastConnetionDateCookie);

            Cookie creationDateCookie = createCookie("creationDate", userCreationDate);
            response.addCookie(creationDateCookie);

            Cookie servicesCookie = createCookie("services", StringUtils.join(services, "|"));
            response.addCookie(servicesCookie);

            _log.info(response);
Ajax code :
function setUserCookies() {
    JQ.ajax({
        type: 'POST',
        url: 'http://.....',
        data: formToJSON,
        contentType:'application/json',
        /* xhrFields: {
            withCredentials: true
        }, */
        crossDomain: true,
        success: function(data, textStatus, jqXHR){
             console.log('Cookie created successfully');
          },
        error: function(jqXHR, textStatus, errorThrown){
             console.log('setUserCookies error : ' + textStatus);
        }
    });
}

function formToJSON() {
    return ${userCookies};
}