浏览器跨域

58 阅读1分钟

由于浏览器的同源策略限制,想不同源(不同协议,不同域名,不同端口)发送ajax请求会失败

image.png

修改响应路径

image.png

image.png

image.png

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server:{
    proxy:{
      '/api':{//获取主路径中包含的/api的请求
        target:'http://localhost:8080',//后天服务所在的源
        changeOrigin:true,//修改源
        rewrite:(path)=>path.replace(/^\/api/,'')//api替换为''
      }
    }
  }
})