vue项目中是怎么传递cookie的
在Vue项目中传递cookie可以通过设置axios的withCredentials属性来实现。具体步骤如下:
- 在Vue项目中安装axios:
npm install axios --save
- 在需要使用axios的组件中引入axios:
import axios from 'axios'
- 在axios的请求配置中设置withCredentials为true:
axios.defaults.withCredentials = true
- 发送带有cookie的请求:
axios.get('http://example.com/api/data', { withCredentials: true })
.then(response => {
console.log(response.data)
})
.catch(error => {
console.log(error)
})
在请求中设置withCredentials为true,会在请求头中增加一个Cookie字段,将浏览器的cookie发送给服务器。
原文地址: https://www.cveoy.top/t/topic/MNy 著作权归作者所有。请勿转载和采集!