panthro's avatar

Using Axios / Boostrap

In my JS I am doing:

import axios from 'axios';

Then i'm using axios in the usual fashion.

But i've also seen this in the bootstrap file:

import axios from 'axios';

window.axios = axios;

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
  1. As I am importing axios again I presume that I am not using the bootstrap version of axios with the default headers config?

  2. What is the purpose of this config?

  3. If I want to use the bootstrap version of axios, instead of importing it to my file, should I do window.axios to make a axios call?

0 likes
6 replies
gych's avatar

If you want to use the axios import from bootstrap you can just use axios in your other js files. You don't need to use window.axios

1 like
panthro's avatar

@gych ok so I just use import axios from 'axios'; in my other files?

Also, what is the purpose of setting window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';.

gych's avatar

@panthro You don't need to use import axios from 'axios'; in other files because the import in bootstrap js will already work in other js files because the import of boostrap.js in app.js

This window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';. add X-Requested-With header to all axios request with as value XMLHttpRequest. If you want detailed info on what XMLHttpRequest does check this page: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

panthro's avatar

@gych thanks but if i remove the import from each of my files that use axios it errors?

Please or to participate in this conversation.