怎样实现跨域AJAX请求发送Cookie

第一步: 服务器必须在Response Header中设置: Access-Control-Allow-Credentials: true

第二步: 客户端发起请求时需要将 xhr.withCredentials设为: true;

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);

 

注意: 

1. 按照上面设置后, 不仅Cookie会被附带上, 认证的头信息也会被附带;

2. 按照上面设置后, 还可以设置远程主机指定的Cookie, 否则就算远程主机想设置, 浏览器也会忽略;

 

posted on 2019-09-20 15:10  aisowe  阅读(747)  评论(0编辑  收藏  举报

导航