vue-cli 中如何使用 proxyTable 进行跨源请求?

How can I use proxyTable to make cross origin request in vue-cli?

我想在我的 vue-cli 项目中使用带有 axios 的 proxyTable。

在我的 config/index.js 中,我输入了这样的代码:

proxyTable: {
  '/getnews': {
    target: 'https://xx.xxx.xx.x'
    changeOrigin: true,
    secure: false,
    pathRewrite: {
      '^/getnews': '/getnews'
    }
  }
}

在我的请求函数中,它是这样的:

var url = '/getnews';
  this.$axios({
    methods: 'get',
    url: url,
  })
  .then(response => {
    console.log(response);
  })

现在问题来了,我的浏览器控制台告诉我

xhr.js?ec6c:178 GET http://localhost:8080/getnews 504 (Gateway Timeout)

终端显示:

Error occurred while trying to proxy request /getnews from localhost:8080 to https://xx.xxx.xx.x (ECONNREFUSED)

所以看起来代理不起作用,我的请求仍然转到我的本地主机。有人知道如何解决这个问题吗?

在朋友的帮助下,我终于弄明白了。 导致问题的是缺少端口号。代码应该是这样的:

proxyTable: {
  '/getnews': {
    target: 'https://xx.xxx.xx.x:8080'
    changeOrigin: true,
    secure: false,
    pathRewrite: {
      '^/getnews': '/getnews'
    }
  }
}

然后就可以正常工作了。