vue项目,使用axios访问豆瓣的API,出现跨域问题。参考了网上的方法,config/index.js的dev中增加如下配置
proxyTable: {
'/api': {
target: 'http://api.douban.com/v2',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
发送请求的代码如下
export default {
name:"Douban",
created:function(){
this.$axios.get('/api/movie/in_theaters')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}
npm run dev启动项目后,无法获取数据。浏览器请求504错误,
终端信息如下
I Your application is running here: http://localhost:8080
[HPM] Error occurred while trying to proxy request /movie/in_theaters from localhost:8080 to http://api.douban.com/v2 (ENOTFOUND) (https://nodejs.org/api/errors.html#errors_common_system_errors)
代理配置完成之后 把启动本地服务的locahost 换为本地的ip启动服务,这是服务端那边做的某些限制
步骤详细如下:
第一步 访问项目的locahost 变成你本地的ip

第二步 配置代理