简体   繁体   English

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

[英]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应用程序,请尝试在您的生产和/或开发环境中设置publicPathbaseUrl

https://cli.vuejs.org/config/#publicpath https://cli.vuejs.org/config/#publicpath

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 Vue.js 中将带有一些数组对象值的 Post 请求发送到我的后端 - How do i send Post request with some values of array object to my Back-end in Vue.js 尝试从 localhost:8080 向 localhost:3000 发出 GET 请求,但我收到 cors 错误,并在我的 node.js 服务器上安装 cors 无法修复它 - Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it Res 值为 null in an app.get call done from vue.js 前端到表达后端 - Res value is null in an app.get call done from vue.js front-end to express back-end 如何从通过 Fetch API 发送的后端 (localhost) 获取数据? (PHP、Fetch、React Native、POST 请求) - How do I fetch data from my back-end (localhost) that's being sent through Fetch API? (PHP, Fetch, React Native, POST request) 使用 localhost 上的证书访问 api(vue.js 应用程序) - Access to api with certificate on localhost (vue.js app) Vue.js中后端API数据值发生变化如何自动更新前端? - How to update the front-end automatically if the back-end API data value changes in Vue.js? 由于 axios 获取请求,无法编译 vue.js 应用程序 - Failed to compile vue.js app due to axios get request 在 vue.js 中使用 axios 打印来自 http GET 请求的响应? - Print response from http GET request using axios in vue.js? 删除请求 API Axios Vue.js - Delete Request API Axios Vue.js Vue.js 对 API 的 GET 请求被 CORS 阻止 - Vue.js GET request to API gets blocked by CORS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM