在Vue js中为Axios设置baseUrl发出请求 [英] Setting baseUrl for Axios in Vue js sends out request
问题描述
在我的应用程序中,我使用Axios来处理我的API请求.在main.js中,我设置了baseUrl.没有我提出请求,它仅通过设置baseUrl即可单独发出请求,因此始终在将应用程序加载到浏览器中时进行.
In my app I use Axios to handle my API-requests. In main.js I have set the baseUrl. Without me making a request it makes a request on its own just by setting the baseUrl, so always when the app is loaded in the browser.
在我的main.js axios.defaults.baseURL = 'https://domain.nl/path/to/my/api
In my main.js axios.defaults.baseURL = 'https://domain.nl/path/to/my/api
一切正常,但是对baseUrl的请求会返回500,表示不请求数据.
All works fine but a request to the baseUrl gives back a 500 for not requesting data.
main.js中的实现:
The implementation in main.js:
import axios from 'axios'
import VueAxios from 'vue-axios'
config:
axios.defaults.baseURL = https://domain.nl/path/to/my/api
axios({
withCredentials: true,
credentials: 'same-origin',
headers: { 'content-type': 'application/x-www-form-urlencoded' }})
然后在另一个文件中执行一个export default {}
,其中包含:
Then in another file I do an export default {}
which contains:
getLang(payload) {
payload.method = 'GET'
payload.url = 'lang';
return axios(payload).then(result => {
return result.data
});
}
有人知道如何取消此请求,还是有更好的方法来设置没有此"bug"的baseUrl?
Does anyone know how to cancel this request or is there a better way to set the baseUrl without this 'bug?
推荐答案
我试图复制您的问题,但是对我来说很好,您如何实现Axios?您使用的是哪个版本?
I have tried to replicate your problem but it works fine to me, how are you implementing Axios? Which version are you using?
建议的解决方案
将此作为Nuxt插件
const axios = require('axios')
module.exports = axios.create({
baseURL: 'domain.nl/path/to/my/api'
})
您可以在下面的评论中找到更多信息
You can find more info in the comments below
这篇关于在Vue js中为Axios设置baseUrl发出请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!