You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1.0 KiB

跨域问题解决方案

配置说明

本项目已配置Vite代理来解决跨域问题。

1. Vite配置 (vite.config.js)

server: {
  host: '0.0.0.0',
  port: 3000,
  proxy: {
    '/api': {
      target: 'https://dbqb.nfdxy.net',
      changeOrigin: true,
      secure: false,
      rewrite: (path) => path.replace(/^\/api/, '/devLotApi/api')
    }
  }
}

2. 请求配置 (src/utils/request.js)

  • baseURL: '/api' - 设置基础URL为代理路径

3. API调用 (src/api/API.js)

  • 使用相对路径 /prize/list 而不是完整的URL
  • 实际请求会被代理到 https://dbqb.nfdxy.net/devLotApi/api/prize/list

工作原理

  1. 前端发起请求到 /api/prize/list
  2. Vite开发服务器拦截请求
  3. 代理将请求转发到 https://dbqb.nfdxy.net/devLotApi/api/prize/list
  4. 服务器响应通过代理返回给前端

注意事项

  • 此配置仅在开发环境有效
  • 生产环境需要在服务器端配置CORS或使用nginx代理
  • 确保目标服务器允许跨域请求