【问题标题】:How to avoid using the Web context in axios.get如何避免在 axios.get 中使用 Web 上下文
【发布时间】:2020-04-09 20:35:15
【问题描述】:


我正在运行一个后端服务,它在这个地址返回一些数据:http://localhost:8080/vuejs-demo/rest/customers
然后我有一个 index.html 页面,位于:http://localhost:8080/vuejs-demo/index.html,它从 REST 服务获取数据:

listUsers: function() {
    axios.get("/vuejs-demo/rest/customers", {

        })
        .then(response => {
            this.users = response.data
        })
}

问题是,我不想硬编码根 Web 上下文信息“vuejs-demo”。 由于 index.html 页面和 REST 服务都在“vuejs-demo”Web 上下文中,我假设我可以直接调用“rest/customers”端点。 但是使用:

    axios.get("rest/customers", {

        })
        .then(response => {
            this.users = response.data
        })
} 

未能包含 Web 上下文,导致 http://localhost:8080//vuejs-demo/rest/customers 同样使用“/rest/customers”会导致http://localhost:8080/rest/customers,这也会导致404错误。 有没有办法解决这个问题? 谢谢

【问题讨论】:

    标签: javascript vue.js axios


    【解决方案1】:

    导入 axios 后,你可以set some defaults,其中一个是基本 url:

    import axios from 'axios'
    axios.defaults.baseURL = 'http://localhost:8080/vuejs-demo/rest';
    

    【讨论】:

      猜你喜欢
      • 2013-03-03
      • 2022-01-03
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2018-08-29
      • 2013-02-15
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多