跨域请求携带cookie

$.ajax({
        url : 'http://remote.domain.com/corsrequest',
        data : data,
        dataType: 'json',
        type : 'POST',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        contentType: "application/json",
        ...

  通过设置 withCredentials: true ,发送Ajax时,Request header中便会带上 Cookie 信息。

对应客户端的 xhrFields.withCredentials: true 参数,服务器端通过在响应 header 中设置 Access-Control-Allow-Credentials = true 来运行客户端携带证书式访问。通过对 Credentials 参数的设置,就可以保持跨域 Ajax 时的 Cookie。这里需要注意的是:

服务器端 Access-Control-Allow-Credentials = true时,参数Access-Control-Allow-Origin 的值不能为 '*' 

关于axiso的跨域携带cookie见https://www.cnblogs.com/yihuite-zch/p/10670364.html

 
原文地址:https://www.cnblogs.com/yihuite-zch/p/10825764.html