Vue 中配置跨域的配置在 vue.config.js 文件中添加:
module.exports = {
devServer: {
proxy: 'http://localhost:3000' // 配置访问的服务器地址
}
}
配置多个地址
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:4000', // 配置访问的服务器地址
pathRewrite: { '^/api': '' }, // 用于将请求中的 /api 字符串替换为空, 然后访问地址就能正确访问,若不添加此行配置,那么访问地址就变成了: http://localhost:5000/api/request_url,这样的请求就会出现 404 操作
ws: true, // 是否支持 webstocket, 默认是 true
changeOrigin: true // 用于控制请求头中的 host 值, 默认是 ture
},
'/api2': {
target: 'http://localhost:5000', // 配置访问的服务器地址
pathRewrite: { '^/api2': '' }, // 用于将请求中的 /api2 字符串替换为空, 然后访问地址就能正确访问,若不添加此行配置,那么访问地址就变成了: http://localhost:6000/api/request_url,这样的请求就会出现 404 操作
ws: true, // 是否支持 webstocket, 默认是 true
changeOrigin: true // 用于控制请求头中的 host 值, 默认是 ture
}
}
}
}