服务器端
//允许跨域发送cookie
res.setHeader("Access-Control-Allow-Credentials", "true");
// 解决setCookit请求源为*的问题;
res.setHeader("Access-Control-Allow-Origin", req.headers.origin)
客户端
在使用fatch,需要添加option配置
fetch(`http://127.0.0.1:3000/register?user=${use}&psw=${password}`, {
method: "GET",
credentials: 'include' //允许跨域携带cookit发送请求
}).then(res => res.json()).then(res => {
console.log(res);
}
在使用ajax发送请求
客户端需要对xhr对象设置withCredentials:true