Sweet Alert - Ajax 没有确认按钮的呼叫
Sweet Alert - Ajax call without confirm button
如何在没有确认按钮的情况下使用 sweetalert 进行 Ajax 调用,但仍然显示正在加载的 gif?基本上,我希望在 Ajax 调用为 运行 时显示正在加载的 gif。开箱即用的解决方案要求您在调用 Ajax 之前启用确认按钮。
以下显然不起作用:
window.swal({
title: "Checking...",
text: "Please wait",
imageUrl: "images/ajaxloader.gif",
showConfirmButton: false,
allowOutsideClick: false
},
function () {
$.getJSON('myurl').done(function (data) {
swal("", "Good job!", "success");
}).fail(function (jqXHR, textStatus, err) {
swal("", "An error occurred", "error");
});
});
您不需要做任何特别的事情。只需在显示 swal 后进行 ajax 调用,并在 ajax 完成后再次调用 swal。
window.swal({
title: "Checking...",
text: "Please wait",
imageUrl: "images/ajaxloader.gif",
showConfirmButton: false,
allowOutsideClick: false
});
//using setTimeout to simulate ajax request
setTimeout(() => {
window.swal({
title: "Finished!",
showConfirmButton: false,
timer: 1000
});
}, 2000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" />
基于的实现示例:
使用 vue-sweetalert2、VueJS 2 和 Axios,并内置加载器动画:
let self = this;
this.$swal.fire({
title: "Processing Request...",
text: "Please wait",
onBeforeOpen() {
self.$swal.showLoading(); //Adds built in loader animation during modal open
},
onAfterClose() {
self.$swal.hideLoading(); //might not be necessary
},
allowOutsideClick: false, //makes modal behave captively
allowEscapeKey: false,
allowEnterKey: false
});
axios
.post(`/api/myapi`, {"fake": "fake"})
.then(r => {
this.$swal.update({
title: "Finished",
html: `Response data: ${r.data}`
});
this.$swal.hideLoading(); //Disables loader spinner, enables button
})
.catch(err => {
this.$swal.update({
title: "FAILED!",
text: "Something went wrong - either try again or contact support."
});
this.$swal.showValidationMessage(`Request failed: ${err}`);
this.$swal.hideLoading(); //Disables loader spinner, enables button
})
如何在没有确认按钮的情况下使用 sweetalert 进行 Ajax 调用,但仍然显示正在加载的 gif?基本上,我希望在 Ajax 调用为 运行 时显示正在加载的 gif。开箱即用的解决方案要求您在调用 Ajax 之前启用确认按钮。
以下显然不起作用:
window.swal({
title: "Checking...",
text: "Please wait",
imageUrl: "images/ajaxloader.gif",
showConfirmButton: false,
allowOutsideClick: false
},
function () {
$.getJSON('myurl').done(function (data) {
swal("", "Good job!", "success");
}).fail(function (jqXHR, textStatus, err) {
swal("", "An error occurred", "error");
});
});
您不需要做任何特别的事情。只需在显示 swal 后进行 ajax 调用,并在 ajax 完成后再次调用 swal。
window.swal({
title: "Checking...",
text: "Please wait",
imageUrl: "images/ajaxloader.gif",
showConfirmButton: false,
allowOutsideClick: false
});
//using setTimeout to simulate ajax request
setTimeout(() => {
window.swal({
title: "Finished!",
showConfirmButton: false,
timer: 1000
});
}, 2000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" />
基于
使用 vue-sweetalert2、VueJS 2 和 Axios,并内置加载器动画:
let self = this;
this.$swal.fire({
title: "Processing Request...",
text: "Please wait",
onBeforeOpen() {
self.$swal.showLoading(); //Adds built in loader animation during modal open
},
onAfterClose() {
self.$swal.hideLoading(); //might not be necessary
},
allowOutsideClick: false, //makes modal behave captively
allowEscapeKey: false,
allowEnterKey: false
});
axios
.post(`/api/myapi`, {"fake": "fake"})
.then(r => {
this.$swal.update({
title: "Finished",
html: `Response data: ${r.data}`
});
this.$swal.hideLoading(); //Disables loader spinner, enables button
})
.catch(err => {
this.$swal.update({
title: "FAILED!",
text: "Something went wrong - either try again or contact support."
});
this.$swal.showValidationMessage(`Request failed: ${err}`);
this.$swal.hideLoading(); //Disables loader spinner, enables button
})