Missing set-cookie header on POST in Spring MVC 3.1

Cookies added to the HttpServletResponse during the call to $ .ajax POST are not displayed in the response header (there is no cookie set). The same code works correctly during GET requests.

I have the following code in a postHandle interceptor:

public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
.
.
            Cookie cookie = new Cookie(User.USER_KEY, userAsJson);
            LOGGER.info("Cookie json is: " + userAsJson);
            cookie.setPath("/");
            response.addCookie(cookie);
            LOGGER.info("Header names: " + response.getHeaderNames());
            LOGGER.info("Set-cookie header(s): " + response.getHeaders("Set-Cookie"));
}

I see this problem when returning from a request for this mapping:

@RequestMapping(value = "/api/user/wait", method = RequestMethod.POST)
@ResponseBody
public User waitingApi(HttpSession session) {

Ajax call parameters:

    var ajaxMessage = {
        url : '/api/user/wait',
        type : 'POST',
        success : waitCallback,
        error : waitErrorCallback
    };

In GET, I see the following in my logs:

Cookie json: {my actual json object}

Header Names: [Set-Cookie]

Set-cookie header: [user = "{my actual json object}"; Version = 1; Path = /]

In POST, I see the following in my logs:

Cookie json: {my actual json object}

Header Names: [Content-Type, Transfer-Encoding, Date, Server]

Set-cookie header: [] <--- this is empty, not edited

+4
1

, Google, : http://mjremijan.blogspot.ca/2012/06/spring-not-setting-cookie-on-ajax.html

, postHandle , , @ResponseBody. cookie , addCookie .

+2

Source: https://habr.com/ru/post/1532550/


All Articles