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) {
//
}
})
原文地址:https://www.jb51.cc/ajax/161132.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。