One issue I faced is that when axios encounters an error, .then()
(used to display success message) doesn't work. So, I decided to include .catch()
to handle errors. But how can I structure the function to both catch errors and implement .then()
for successful requests?
I am looking to integrate the following code snippet:
.then(() =>
{
swal({
title: "Message sent successfully!",
type: "success"
}).then(function(){
location.reload()
})
})
Into this existing function:
axios.post('http://localhost:5291/api/Mail', {
"to": url,
"subject": document.getElementById("ContactSubject").value,
"body": `<h3>From:</h3> ${document.getElementById("ContactEmail").value} <br>
<h4>Name:</h4> ${document.getElementById("ContactName").value} <br>
<h4>Message:</h4> ${document.getElementById("ContactMessage").value}`
}).catch((error)=> {
swal("Something went wrong!" , `${error.message}` , "error")
.then(function(){
location.reload()
})
})