axios fail code example
Example 1: 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 2: axios error
axios.get('/api/xyz/abcd')
.catch(function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
});
Example 3: axios
const axios = require('axios');
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
async function getUser() {
try {
const response = await axios.get('/user?ID=12345');
console.log(response);
} catch (error) {
console.error(error);
}
}