vue项目前端设置跨域不生效

有一个服务的请求后端没有设置跨域
前端设置跨域不生效
在vue.config.js里面这样设置

module.exports = {
  devServer: {
    proxy:'http://10.11.5.166:132',
  },
}

页面请求

axios({
  url:url,
  method:'post',
  params:{
    image_type:this.size_dict,
      road_type:this.road_type,
  },
  headers: { 'content-type': 'application/x-www-form-urlencoded' },
  data,
}).then(res => {
  console.log(res);
})

image.png
前端设置跨域不生效,请问有什么办法吗,除了让后端配置

阅读 2.9k
2 个回答

代理到同域名下(http://localhost:8080)就行了。

module.exports = {
  devServer: {
    proxy: {
        '/segmentation': {
            target: 'http://10.11.5.166:132'
        }
    },
  },
}

proxy的配置应该有个path参数的。可参考如下配置:

module.exports = {
  devServer: {
    port: 8080,
    hotOnly: true,
    proxy: {
      '^/segmentation': {
        target: 'http://10.11.5.166:132',
        ws: true,
        changeOrigin: true
      }
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题