ajax cookie跨域

前端之家收集整理的这篇文章主要介绍了ajax cookie跨域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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.f2er.com/ajax/161132.html

猜你在找的Ajax相关文章