问题描述
mui前端通过ajax的ip申请访问servlet,我发现每次申请都会产生一个新的session 导致session无法使用
(原生java)
解决办法
首先我们在ajax中加一句话,告诉浏览器传递sessionid过去
// An highlighted block
xhrFields: {withCredentials: true},
然后在servlet中加入一下几句话
// An highlighted block
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");//是否支持跨域
response.setHeader("Access-Control-Allow-Methods", "GET,POST");
完美解决