axios .catch code example
Example 1: get json data when we get error code in axios
axios.post('/formulas/create', {
name: "",
parts: ""
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});
Example 2: axios try catch get error status cocxe
try {
await axios.get('/bad-call')
} catch (error) {
const err = error as AxiosError
if (err.response) {
console.log(err.response.status)
console.log(err.response.data)
}
this.handleAxiosError(error)
}
Example 3: vue axios catch error
axios.get('/user/1').then((response) => {
console.log('Everything is awesome.');
}).catch((error) => {
console.warn('Not good man :(');
})
Example 4: axios get status code
axios.get('/foo')
.catch(function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
}
});
Example 5: axios
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>