Cors 跨域
Nginx 配置
txt
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
#add_header 'Access-Control-Allow-Credentials' 'true';
vite 项目配置
txt
vite.config.ts 配置
server: {
host: "0.0.0.0",
port: 3000,
cors: true,
}
axios 配置
ts
const config = {
// 默认地址请求地址,可在 .env.** 文件中修改
baseURL: import.meta.env.VITE_API_URL as string,
// 设置超时时间
timeout: 20000,
// 跨域时候允许携带凭证 (默认withCredentials: false)
// withCredentials: true
}