问题描述
下面是我的AJAX调用之一的代码。当进入else
条件时,SweetAlert代码不会以该代码格式触发。我不知道为什么当我使用此代码时,它可以正常工作:
swal("Task is still in progress","Wait for status change to Done","warning");
由于我要重定向到单击swal上的OK的页面,因此无法使用此代码。 可能是什么原因?
if (placeholder4Status == 'Done') {
$.ajax({
type: "GET",url: "/addTasks-approve",dataType: "json",data: {
text: text,id: id
},contentType: "application/json",success: function(response) {
if (response.status == '200') {
window.location.replace('/tasksAdmin');
$.LoadingOverlay("hide");
}
}
});
} else {
$.LoadingOverlay("hide");
// swal("Task is still in progress","Wait for status change to Done ","warning");
swal({
title: "Task is still in progress!",text: "Please wait for status change to Done ",type: "warning",showCancelButton: true
},function() {
window.location.href = '/tasksAdmin';
// window.location.replace('/tasksAdmin');
});
return false;
}
解决方法
放置执行window.location.href
的函数并不十分正确。相反,您可以尝试使用Promises更改代码,这是一个示例:
swal({
title: "Task is still in progress!",text: "Please wait for status change to Done ",type: "warning",showCancelButton: true
}).then((result) => {
if (result) {
window.location.href = '/tasksAdmin';
}
});