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