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.

43 lines
1.0 KiB

4 weeks ago
  1. # 跨域问题解决方案
  2. ## 配置说明
  3. 本项目已配置Vite代理来解决跨域问题。
  4. ### 1. Vite配置 (vite.config.js)
  5. ```javascript
  6. server: {
  7. host: '0.0.0.0',
  8. port: 3000,
  9. proxy: {
  10. '/api': {
  11. target: 'https://dbqb.nfdxy.net',
  12. changeOrigin: true,
  13. secure: false,
  14. rewrite: (path) => path.replace(/^\/api/, '/devLotApi/api')
  15. }
  16. }
  17. }
  18. ```
  19. ### 2. 请求配置 (src/utils/request.js)
  20. - `baseURL: '/api'` - 设置基础URL为代理路径
  21. ### 3. API调用 (src/api/API.js)
  22. - 使用相对路径 `/prize/list` 而不是完整的URL
  23. - 实际请求会被代理到 `https://dbqb.nfdxy.net/devLotApi/api/prize/list`
  24. ## 工作原理
  25. 1. 前端发起请求到 `/api/prize/list`
  26. 2. Vite开发服务器拦截请求
  27. 3. 代理将请求转发到 `https://dbqb.nfdxy.net/devLotApi/api/prize/list`
  28. 4. 服务器响应通过代理返回给前端
  29. ## 注意事项
  30. - 此配置仅在开发环境有效
  31. - 生产环境需要在服务器端配置CORS或使用nginx代理
  32. - 确保目标服务器允许跨域请求