I don't know how to get the response of a successful Axios call and display the success message.
There is a button named "Change Workflow Order", which has an onClick function.
<div style="display: flex"> <button class="btn btn-info change-workflow" onclick="changeWorkflowOrder()"> Change Workflow Order </button> <span style="color: green; margin-left: 5px;" id="alert_msg"> </span> </div>
This is the onClick function. The changeWorkflowOrder function runs an Axios call.
function changeWorkflowOrder() { const response = axios.post(js_site_url + '/sort-detail', $('#sortable').sortable("toArray"), { headers: { 'Content-Type': 'multipart/form-data' } }) }
I want to capture the successful response of the Axios call and if the response of the Axios call is successful, display the success message in the HTML span tag. The message must only be displayed for 4 seconds. After 4 seconds, the success message must be hidden.
I try to create this. This is the changeWorkflowOrder function I tried.
function changeWorkflowOrder() { const response = axios.post(js_site_url + '/sort-detail', $('#sortable').sortable("toArray"), { headers: { 'Content-Type': 'multipart/form-data' } }); response.then((result) => { $("#alert_msg").html(result.data.success ? "Success" : "Order change successfully"); clearTimeout(timeoutId); timeoutId = setTimeout(function() { $("#alert_msg").hide(); }, 4000); }); }
This code displays the error message correctly. But after 4 seconds the error message is shown, the "Order change successfully" message is not hidden, I think the Axios call response capture part is incorrect.
How can I capture the response of an Axios call and display a success message for 4 seconds in an HTML span tag.
I recommend first creating an axios client for your api/webservice:
Then rewrite your onclick function and implement delay, refer to this Stack Overflow answer :