此種解決方案基于vue-cli大家的vue項(xiàng)目
配置proxyTable
1.打開condfig/index.js
2.在dev配置對(duì)象中找到proxyTable:{}
3.添加如下配置
// 配置反向代理钙皮,解決跨域請(qǐng)求
proxyTable: {
'/api': {
target: 'http://www.ajax.cn',
changeOrigin: true,
pathRewrite: {
'^/api': '/api'
}
}
},
4.重啟開發(fā)服務(wù)器npm run dev
發(fā)送請(qǐng)求示例
// 導(dǎo)入axios
import axios from 'axios'
axios.defaults.baseURL='http://localhost:8080'
axios.get('/api/json.php').then(function(res){
console.log(res);
});