js exios request code example

Example 1: axios cdn

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

Example 2: install axios

npm install axios

Example 3: axios.interceptors.response.use

// Add a response interceptor
HTTP.interceptors.response.use(function (response) {
 return response
}, function(error) {
 if (error.response.status === 401) {
  store.dispatch('logout')
  router.push('/login')
 }
 return Promise.reject(error.response.data)
})

Example 4: making axios call with headers

axios.post('url', {"body":data}, {
    headers: {
    'Content-Type': 'application/json'
    }
  }
)

Tags:

Misc Example