当前位置:   article > 正文

使用 axios 设置请求带上cookie_全局axios配置开启cookie功能

全局axios配置开启cookie功能
import axios from 'axios'
  • 1

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)})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

服务端响应头设置

响应需要携带响应头 Access-Control-Allow-Credentials: true
  • 1

在这里插入图片描述

响应头 Access-Control-Allow-Origin 不能是通配符"*" ,并且需要和请求头Origin 的值一致
  • 1

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号