这个是config中index.js添加proxyTable
proxyTable: {
'/research': {
target: 'https://api.shenjian.io/?appid=c36a22e564a2954e&keyword=a&pageNo=1&pageSize=20',
//目标接口域名
changeOrigin: true, //是否跨域
pathRewrite: {
'^/research': '/c' //重写接口
}
}
}
api中index.js添加接口
export default {
songsearch: '/research'
}
store中调用ajax函数
mutations: {
changeKeyword(state, x) {
ajax(require.songsearch)
}
}
ajax
const ajax = function (url) {
return new Promise(function (resolve, reject) {
axios.get(url).then(res => {
resolve(res);
}).catch(reject);
});
}
这个是结果
刚开始学习vue,想做一个web播放器而且已经拿到api,但是axios请求需要跨域,所以用proxyTable的方法进行代理,但是请求地址变成了端口号,为什么?
你这样配置完,访问到的接口是 https://api.shenjian.io/?appi... 这样肯定要报404啊。
还有就是在浏览器中,虽然你看到的是localhost:8080,但其实已经经过代理转发到你要请求的target了