Change the default base url for axios

前端 未结 4 1283
耶瑟儿~
耶瑟儿~ 2021-02-01 12:42

I have configured my axios like this

const axiosConfig = {
  baseURL: \'http://127.0.0.1:8000/api\',
  timeout: 30000,
};

Vue.prototype.$axios = axios.create(ax         


        
4条回答
  •  醉话见心
    2021-02-01 13:18

    Putting my two cents here. I wanted to do the same without hardcoding the URL for my specific request. So i came up with this solution.

    To append 'api' to my baseURL, I have my default baseURL set as,

    axios.defaults.baseURL = '/api/';
    

    Then in my specific request, after explicitly setting the method and url, i set the baseURL to '/'

    axios({
        method:'post',
        url:'logout',
        baseURL: '/',
       })
       .then(response => {
          window.location.reload();
       })
       .catch(error => {
           console.log(error);
       });
    

提交回复
热议问题