[英]Vue.js app is run on http://localhost:8080, whenever I make a GET request using axios to my back-end API, the localhost gets prepended to the URL
I have set axios's base URL to be a variable from my.env file like this:我已将 axios 的基础 URL 设置为 my.env 文件中的变量,如下所示:
axios.defaults.baseURL = process.env.VUE_APP_SERVER_API
And this is the .env
variable VUE_APP_SERVER_API="http://lolapi.example.com/api"
这是.env
变量VUE_APP_SERVER_API="http://lolapi.example.com/api"
Whenever I make a GET request like this:每当我发出这样的 GET 请求时:
axios.get('/' + action + 'Summoner/' + trimmedSummoner)
The request goes to the wrong URL.请求转到错误的 URL。 It tries to GET from this URL:它试图从这个 URL 获取:
http://localhost:8080/summoner/lolapi.example.com/api/getSummoner/TheOnyxKing
instead of this one:而不是这个:
http://lolapi.example.com/api/getSummoner/TheOnyxKing
If I hardcode the correct URL in the GET request, everything works fine:如果我在 GET 请求中对正确的 URL 进行硬编码,一切正常:
axios.get('http://lolapi.kabzamalov.com/api/getSummoner/TheOnyxKing')
Any clue why is this happening?任何线索为什么会发生这种情况? Also this doesn't happen if I build the Vue.js project, it only happens during development.如果我构建 Vue.js 项目,这也不会发生,它只会在开发过程中发生。
If this is a vue-cli
app, try setting publicPath
or baseUrl
in your production and/or development environment.如果这是一个vue-cli
应用程序,请尝试在您的生产和/或开发环境中设置publicPath
或baseUrl
。
https://cli.vuejs.org/config/#publicpath https://cli.vuejs.org/config/#publicpath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.