vite跨域问题新手错误

2023-12-20  本文已影响0人  ioido
// vue.config.ts
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, 'api')
      }
    },
  }
})

// 其他view文件中请求url这样写
function getData() {
  axios.get('/api/user/getAllUsers') // 或者 http://localhost:5173/api/user/getAllUsers 而不是 http://localhost:80/api/user/getAllUsers 想代理成的网址
      .then(res => {
        console.log(res)
      })
}
上一篇 下一篇

猜你喜欢

热点阅读