【vue】代理机制
最近写node返回json,
正常情况下,请求localhost:3500就会看到结果:
因为请求的时候端口不一致,在前端请求的时候会出现跨域请求的问题:
How?
在config/index里边添加
proxyTable: {
"/api": {//使用api来替代http://localhost:3500
target: "http://localhost:3500",
changeOrigin: true,
pathRewrite: {//路径重写
'^/api': "/" //最后拼接出来的地址为http://localhost:3500/
//'^/api':"/api" //最后拼接出来的地址为http://localhost:3500/api
}
}
},
然后前端请求:
import axios from 'axios'
export default {
name: 'App',
created(){
this.getdata();
},
methods:{
getdata(){
//正常的请求发起
axios.get("/api")
.then(response => {
console.log("====",response.data);
}
,error =>{
console.log("=====",error);
});
}
}
最后请求成功: