Front-end solves cross-domain CORS setting proxy

When a CORS cross-domain problem occurs in the request address, the solution is:

1. Add an /api (can be called something else) character as a judgment 

 

Then set the proxy in vue.config.js (vue.config.js is a configuration file created by yourself in the root directory of vue3, vue2 has a corresponding configuration file)

 devServer.proxy configuration 

 '/api' : {
                target: "http://121.41.14.175:10521", //请求目的ip: 端口
                // ws: true,//表示是否要开通链接websoket请求
                changeOrigin: true,
                pathRewrite: {
                    '^/api':''//这里理解成用‘/api’代替target里面的地址,
                    //后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
                }
            }

Request successful! 

Guess you like

Origin blog.csdn.net/weixin_44220845/article/details/123667022