ajax cookie跨域

来源:互联网 发布:php经典书籍推荐 编辑:程序博客网 时间:2024/06/05 20:04

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) {           //        }    })
原创粉丝点击