根据同源策略,不同的协议/域名/端口不能读取对方资源
vue+axios解决跨域方案
1.根目录创建vue.config.js文件
2.修改proxy(上面的文件夹里编写)
module.exports = {
devServer: {
proxy: {
'/api': {
target: '', // 请求的地址
changeOrigin: true, // 是否跨域
pathRewrite: {
'^/api': '/api' // 替换target中的请求地址
}
}
}
}
}
3.请求
this.$axios.post("接口名", {
id:''
})
.then(function (res) {
})
.catch(function (err) {
});