在 Vue3 中,可以使用 axios 库来发送请求并携带 Cookie。

安装 axios 库

首先,需要安装 axios 库:

npm install axios

发送请求并携带 Cookie

然后,在发送请求时,可以通过 axios 的配置项来携带 Cookie。例如:

import axios from 'axios';

axios.get('/api/data', {
  withCredentials: true // 携带cookie
}).then(response => {
  console.log(response.data);
}).catch(error => {
  console.log(error);
});

其中,withCredentials 配置项设置为 true 表示携带 Cookie。

服务器设置

注意,服务器也需要设置 Access-Control-Allow-Credentialstrue 才能接收到携带 Cookie 的请求。

Vue3 发送请求携带 Cookie:完整指南和示例

原文地址: https://www.cveoy.top/t/topic/mWjy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录