最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting baseUrl for Axios in Vue js sends out request - Stack Overflow

programmeradmin3浏览0评论

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.

In my main.js axios.defaults.baseURL = '

All works fine but a request to the baseUrl gives back a 500 for not requesting data.

The implementation in main.js:

import axios from 'axios'
import VueAxios from 'vue-axios'

config:

axios.defaults.baseURL = 
axios({
withCredentials: true,
credentials: 'same-origin',
headers: { 'content-type': 'application/x-www-form-urlencoded' }})

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
    });
}

Does anyone know how to cancel this request or is there a better way to set the baseUrl without this 'bug?

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.

In my main.js axios.defaults.baseURL = 'https://domain.nl/path/to/my/api

All works fine but a request to the baseUrl gives back a 500 for not requesting data.

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' }})

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
    });
}

Does anyone know how to cancel this request or is there a better way to set the baseUrl without this 'bug?

Share Improve this question edited Aug 29, 2018 at 20:19 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Jul 16, 2018 at 10:30 user3918528user3918528 1451 gold badge1 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I have tried to replicate your problem but it works fine to me, how are you implementing Axios? Which version are you using?

[EDIT] Suggested solution

Using this as Nuxt plugin

const axios = require('axios')
module.exports = axios.create({
  baseURL: 'domain.nl/path/to/my/api'
})

You can find more info in the ments below

import Vue from "vue";
import Axios from "axios";


Vue.prototype.$http = Axios;

Axios.defaults.headers.mon = {'X-Requested-With': 'XMLHttpRequest'}
Axios.defaults.baseURL = (process.env.API_PATH !== 'production') ? 'http://localhost:8000/api/' : '';

Ensure baseURL not baseUrl

发布评论

评论列表(0)

  1. 暂无评论