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 的值一致

在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐