最近在一个项目中,处理登录,登录是通过session存储用户信息,对于同一个服务器,登录成功之后,对于axios请求不能正确获取session,抓包之后发现是因为请求没有携带原cookie,因此此处记录一下:
react中:
axios.defaults.withCredentials=true;let data={params:"value"};axios.get(`/api/getData`).then((res) => {//TODO});axios.post(`/api/postData`, qs.stringify(data)).then(res => {//TODO});
vue中:
const service = axios.create({baseURL: process.env.BASE_API, // node环境的不同,对应不同的baseURLtimeout: 5000, // 请求的超时时间//设置默认请求头,使post请求发送的是formdata格式数据// axios的header默认的Content-Type好像是'application/json;charset=UTF-8',我的项目都是用json格式传输,如果需要更改的话,可以用这种方式修改// headers: { // "Content-Type": "application/x-www-form-urlencoded"// },withCredentials: true // 允许携带cookie
如果觉得《axios 跨域携带cookie设置》对你有帮助,请点赞、收藏,并留下你的观点哦!