前端访问http出现跨域的情况,可配置代理转发这个http

方案一:在vite中配置proxy,rewrite会改变转发的url

proxy: {
   "/geoserver": {
     target: "http://192.168.10.182:8080/geoserver",
     changeOrigin: true,
     secure: false,
     rewrite: (path) => path.replace(/^\/geoserver/, ""),
   },
   "/api-service": {
     target: "http://192.168.10.182:8081",
     changeOrigin: true,
     secure: false,
   }
 }

方案二:若前端项目发布,可用ng代理转发

location /api-service/ {
   proxy_pass http://192.168.10.198:8081/api-service/;

   add_header Access-Control-Allow-Origin *;
   add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
   add_header Access-Control-Allow-Headers
       'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

   if ($request_method = 'OPTIONS') {
       return 204;
   }
}

更多推荐