使用 axios 设置请求带上cookie

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39991767/article/details/137040168