为什么这里使用localhost就拿不到后端传来的cookie(JSESSIONID),请求是正常的,resp.data.data可以拿到数据,就是拿不到后端的cookie,换成127.0.0.1就可以;还有就是直接在浏览器的URL那里输localhost是可以的。
axios({
method: 'post',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
withCredentials: true,
url: 'http://localhost:8088/login/getVerificationCode',
data: JSON.stringify(this.user)
})
.then(resp => {
console.log(resp.data.data);
})
.catch(err => {
console.log(err);
})
用localhost会有警告,说是samesite的问题,我查了相关这个词条,大概就是说安全的原因,得同源,要不就上HTTPS,但为什么127.0.0.1就可以
换成127.0.0.1就没警告了,后端的cookie也可以正常拿到
js-cookie: https://www.npmjs.com/package/js-cookie 可以访问这个链接
通过下载 npm install js-cookie --save 先下载下来
然后在组件内引入
import cookies from 'js-cookies'
然后通过set来设置我们的cookie
Cookies.set('name', 'value', { expires: 7 });
然后通过get来获取我们的cookie
Cookies.get('name'); // => 'value'
这时我们访问后台的cookies就可以看到我们存储的cookie了
最后需要在axios请求拦截中设置header头
config.headers['名子']=cookies.get('获取的cookie值 ')