问题:向localhost:8001发送跨域请求
proxy: { // 开发环境代理配置
'/dev-api': {
target: 'http://localhost:8001',
changeOrigin: true, // 开启代理服务器,
pathRewrite: {
'^/dev-api': '',
}
}
}
注意:添加或者更改配置时,需要重新启动一下服务方能生效。
发送请求部分代码(仅展示了部分代码):
const Base_URL = '/dev-api';
export default {
getList() {
const req = request({
method: 'get',
url: Base_URL + '/db.json'
});
return req;
}
}
解决方案:
1、路径问题
target: 'http://localhost:8001'按此格式书写
2、开启服务http://localhost:8001
在跨域访问http://localhost:8001之前需要先将http://localhost:8001的服务打开,确保可以访问http://localhost:8001。