vue3 axios跨域问题
根目录下
vue.config.js
新增配置
module.exports = defineConfig({ devServer: { proxy: { '^/api': { target: 'http://www.xxx.com/',//接口的前缀 ws:true,//代理websocked changeOrigin:true,//虚拟的站点需要更管origin pathRewrite:{ '^/api':''//重写路径 } } } } })
参数说明:
target 你要请求的服务器地址
pathRewrite 重写的路径 也就是 http://www.xxx.com/ 等于 /api
例之前的
axios.post('http://www.xxx.com/goodsList', { firstName: 'Fred', lastName: 'Flintstone' })
配置后变成,http://www.xxx.com/ 等于 /api
axios.post('/api/goodsList', { firstName: 'Fred', lastName: 'Flintstone' })
注意配置完,需要重启vue