vue在开发模式下,怎么改变向后台发送请求的默认地址

向后台发送请求现在的写法

    this.$http.get('http://1.1.1.1:8080/product/getAllProducts?page=0')
      .then((response) => {
        me.tableData = response.body.data.content
      })
      .catch(function (response) {
        /**
         * todo:此处待补充错误处理 
         * */
      })

我希望改成下面写法但是效果还是http://1.1.1.1:8080/product/getAllProducts?page=0

      this.$http.get('product/getAllProducts?page=0')
      .then((response) => {
        me.tableData = response.body.data.content
      })
      .catch(function (response) {
        /**
         * todo:此处待补充错误处理 
         * */
      })

为此,我改了config/index.js文件中的

dev: {
    proxyTable: {
      '/api': {
        target: 'http://1.1.1.1:8080',
        changeOrigin: true
      }
}
   

但是并不能实现我想要的效果,求助求助

注意:我用的是vue-resource,怎么写呢?而且我就是开发模式下请求需要指定地址,在build模式下正常使用localhost

阅读 6.6k
2 个回答

webpack-dev-server可以配置代理,

devServer: {
    proxy: {
        '/some/path*': {
            target: 'https://other-server.example.com',
            secure: false,
        },
    },
},
axios可以全局配置axios.defaults.baseURL = 'http://api.exmple.com';  

axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['content-Type'] = 'appliction/x-www-form-urlencoded';

this.$http.get('product/getAllProducts?page=0') 

请求前面少了一个'/',改成

this.$http.get('/product/getAllProducts?page=0'),

其实可以控制台调试一下,看看接口请求的详细信息,一步一步找原因可以找到的。

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