前端跨域:http-proxy-middleware 代理解决(本项目使用vue-cli4.0脚手架搭建)
1.在vue.config.js中配置:
devServer:{
open:true, /*项目启动时是否自动打开网页*/
host:'localhost', /*域名*/
port:8080, /*端口号*/
https:false, /*https设置*/
hotOnly: false, /*热更新 */
proxy:{ /*跨域配置*/
// 配置多个代理(配置一个 proxy: 'http://localhost:8080' )
"/api": {
target: "http://www.*********",//要用的服务器地址
ws: true,//
changeOrigin: true,
pathRewrite: {
"^/api": "" //请求的时候使用这个api就可以
}
}
},
}
2.使用axios请求数据时直接使用“/api”:
getData () {
axios.get('/api/bj11x5.json', function (res) {
console.log(res)
})