Posts

Showing posts with the label Axios

Change The Default Base Url For Axios

Answer : Instead of this.$axios.get('items') use this.$axios({ url: 'items', baseURL: 'http://new-url.com' }) If you don't pass method: 'XXX' then by default, it will send via get method. Request Config: https://github.com/axios/axios#request-config 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); }); Create .env.development , .env.production files if not exists and add there your API endpoint, for example: VUE_APP_...

Cant Cancel Axios Post Request Via CancelToken

Answer : I have found out that you can cancel post request this way,i missunderstand this documentation part. In previous code,i have passed cancelToken to the POST data request not as a axios setting. import axios from 'axios' var CancelToken = axios.CancelToken; var cancel; axios({ method: 'post', url: '/test', data: { firstName: 'Fred', lastName: 'Flintstone' }, cancelToken: new CancelToken(function executor(c) { // An executor function receives a cancel function as a parameter cancel = c; }) }).then(()=>console.log('success')).catch(function(err){ if(axios.isCancel(err)){ console.log('im canceled'); } else{ console.log('im server response error'); } }); // this cancel the request cancel() Cancel previous Axios request on new request with cancelToken and source. https://github.com/axios/axios#cancellation // cancelToken and source declaration con...

Axios Delete Request With Body And Headers?

Answer : So after a number of tries, I found it working. Please follow the order sequence it's very important else it won't work axios.delete(URL, { headers: { Authorization: authorizationToken }, data: { source: source } }); axios.delete does support a request body. It accepts two parameters: url and optional config. You can use config.data to set the request body and headers as follows: axios.delete(url, { data: { foo: "bar" }, headers: { "Authorization": "***" } }); See here - https://github.com/axios/axios/issues/897 Here is a brief summary of the formats required to send various http verbs with axios: GET : Two ways First method axios.get('/user?ID=12345') .then(function (response) { // Do something }) Second method axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { // Do something }) The two above are equivalent. Observe the params...