vue.js-使用proxyTable转发请求访问豆瓣API,504错误

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)
阅读 6.4k
1 个回答

代理配置完成之后 把启动本地服务的locahost 换为本地的ip启动服务,这是服务端那边做的某些限制

步骤详细如下:

第一步 访问项目的locahost 变成你本地的ip
clipboard.png

第二步 配置代理

 proxyTable: {
  '/v2': {
    target: 'http://api.douban.com',
    changeOrigin: true,
   }

}

this.$axios.get('/v2/movie/in_theaters')
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });   // 就按我写的配不需要更改
    

clipboard.png

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题