Why is jquery’s .ajax() method not sending my session cookie?
Ajax跨域请求如何附带Cookie
automatically add header to every response
后端使用springMVC,前端ajax。
public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,HttpServletResponse response,FilterChain filterChain)
throws ServletException,IOException {
response.addHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
// response.addHeader("Access-Control-Allow-Origin","*");
response.addHeader("Access-Control-Allow-Credentials","true");
if (request.getHeader("Access-Control-Request-Method") != null
&& "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE");
response.addHeader("Access-Control-Allow-Headers","X-Requested-With,Origin,Content-Type,Accept");
}
filterChain.doFilter(request,response);
}
}
$.ajax({
type: 'post',url: urlname+'/user/login',data: data,dataType: "json",xhrFields: {
withCredentials: true
},crossDomain: true,success: function (result) {
// console.log('开始处理服务器端返回的注册结果')
},error: function (XmlHttpRequest,textStatus,errorThrown) {
//
}
})