vu3+vite+axios跨域問題


接口路徑:/api/inpController/getBeds

前台路徑:http://199.199.199.199:3000

后台路徑:http://199.199.199.199:8082

配置過程:

1 axios的baseUrl必須為代理路徑匹配的字符串

axios.create({
  baseURL: '/api',
  timeout: 5000,
  responseType: 'json',
  withCredentials: true,
});

2 接口路徑只需配置接口名

export const getBeds = (data)=> post(data, '/inpController/getBeds')

3 vite.config.js中server配置api的代理

server: {
  host: 'localhost',
  port: 3000, // 端口號
  https: false,
  cors: true, // 默認啟用並允許任何源
  open: true, // 在服務器啟動時自動在瀏覽器中打開應用程序
  //反向代理配置,注意rewrite寫法,開始沒看文檔在這里踩了坑
  proxy: {
    '/api': {
      target: 'http://199.199.199.199:8082', //代理接口
      changeOrigin: true,
      ws: true,
      secure: true,
    }
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM