React 跨域配置

vite+React+antd

配置代理

//vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      '/api': {
        target: 'http://47.120.14.63:8081', //目标url
        changeOrigin: true, //支持跨域
        rewrite: (path) => path.replace(/^\/api/, ""), 
          //重写路径,替换/api
      }
    }
  }
})

 修改axios封装

//request.js
import axios from "axios";

let request = axios.create({
    // baseURL: "http://47.120.14.63:8081",
    baseURL: "/api",
    timeout: 3000
})

例如: 


你可能感兴趣的:(疑难杂症,react.js,javascript,前端)