行业知识
使用 axios 设置请求带上cookie
2026-11-03 07:32:48
来源:互联网转载
import axios from 'axios'
axios设置 withCredentials
1. 在全局设置axios.defaults.headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'withCredentials':true} 2. 在实例上设置// 创建axios实例const service = new axios.create({ 'withCredentials':true}) 3. 在请求时设置axios.post(api_url, data, {'withCredentials':true}).then(function (res) { console.log(res)}).catch(function (e) {console.log(e)}) 服务端响应头设置
响应需要携带响应头 Access-Control-Allow-Credentials: true
响应头 Access-Control-Allow-Origin 不能是通配符"*" ,并且需要和请求头Origin 的值一致
下一篇:怎么补网站漏洞?