I use swal within ajax, but swal do not wait until you make the request, here is my code:
$("#btnSubmit").click(function() {
var data = $("#myForm").serialize();
$.ajax({
type: "POST",
url: myURL,
data: data,
success: function(response) {
swal({
title: "Success",
text: "Data added.",
icon: "success"
})
.then(() => {
window.location.replace(referrer);
});
}
});
});
I haven't had any luck reproducing your issue. See the snippet for an example. Are there any errors in the console? Those would be helpful for us to see.
const btn = document.querySelector('#test'); btn.addEventListener('click', () => { fetchTodos() .then(() => { swal({ title: 'Success', text: "This is in fetchTodos().then()", icon: 'success', }).then(() => console.log('Do something here')); }) }); function fetchTodos() { return fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(json => console.log(json)) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script> <button id="test"> SweetAlert </button>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.