vue前后端分离跨域配置

前后端分离项目开发中、前端请求后端接口时,存在违反同源策略(Same origin policy)是一种约定(协议、域名、端口)。

vue配置代理服务器proxy解决跨域
eg:后端接口:http://localhost:12911

解决:在根目录新建或在vue.config.js中配置

module.exports = {
    devServer: {
        proxy: {
            "/api": {
                target: 'http://localhost:12911', //目标接口域名
                secure: false,            //false为http访问,true为https访问
                changeOrigin: true,      //是否跨域
                //重写路径  
                pathRewrite: {
                    '^/api': 'http://localhost:12911'
                },
            },
    },
 },

页面调用
```axios.post(’/api/user-registration’, params)``

打包后proxy代理失效

Logo

前往低代码交流专区

更多推荐