浏览器安全策略跨域方法CORS怎么携带cookie
在CORS中,浏览器默认不会发送cookie等敏感信息。为了允许跨域请求携带cookie,需要在服务端设置Access-Control-Allow-Credentials为true,并在请求头中设置withCredentials为true。
示例:
1.服务端设置Access-Control-Allow-Credentials为true
response.setHeader("Access-Control-Allow-Credentials", "true");
2.客户端发送请求时设置withCredentials为true
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('GET', 'http://example.com', true);
xhr.send();
原文地址: https://www.cveoy.top/t/topic/bVX7 著作权归作者所有。请勿转载和采集!