如何使用SweetAlert进行AJAX调用

时间:2017-09-22 04:20:28

标签: javascript jquery sweetalert sweetalert2

当用户想要删除帐户时,使用SweetAlert进行AJAX调用。但是我的代码下面没有工作。我还要添加确认和取消按钮,但我不知道如何使用SweetAlert

任何帮助将不胜感激。

我正在使用SweetAlert CDN

来自https://unpkg.com/sweetalert/dist/sweetalert.min.js

https://sweetalert.js.org/

 swal({
   closeOnClickOutside: false,
   icon: "warning",
   title: 'Do you want to remove your account?',
   text: 'This action can not be undo',
   showConfirmButton: false,
   closeOnConfirm: false,
   showSpinner: true
 },function () {
   $.ajax({
     url: "delete_account.php",
     method: "POST",
     data: {
       id: 5
     },
     success: function () {
       swal("Deleted!", "Successfully deleted", "success");
     }
   });
 });

3 个答案:

答案 0 :(得分:0)

您可以尝试以下代码。

setTimeout(function () { 
swal({
  title: "Wow!",
  title: 'Do you want to remove your account?',
   text: 'This action can not be undo',
  confirmButtonText: "OK"
},
function(isConfirm){
  if (isConfirm) {
    $.ajax({
     url: "delete_account.php",
     method: "POST",
     data: {
       id: 5
     },
     success: function () {
       swal("Deleted!", "Successfully deleted", "success");
     }
   });
  }
}); }, 1000);
<script src="https://code.jquery.com/jquery-2.1.3.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">

答案 1 :(得分:0)

ajax呼叫继续点击确认。

   function deleteorder(orderid) {
        swal({
          title: "Are you sure?", 
          text: "Are you sure that you want to cancel this order?", 
          type: "warning",
          showCancelButton: true,
          closeOnConfirm: false,
          confirmButtonText: "Yes, cancel it!",
          confirmButtonColor: "#ec6c62"
        }, function() {
            $.ajax(
                    {
                        type: "post",
                        url: "/admin/delete_order.php",
                        data: "orderid="+orderid,
                        success: function(data){
                        }
                    }
            )
          .done(function(data) {
            swal("Canceled!", "Your order was successfully canceled!", "success");
            $('#orders-history').load(document.URL +  ' #orders-history');
          })
          .error(function(data) {
            swal("Oops", "We couldn't connect to the server!", "error");
          });
        });
       }

Answer from here

答案 2 :(得分:0)

您可以在确认甜蜜警报时调用ajax,如下所示。

swal({
    title: "Are you sure to delete this  of ?",
    text: "Delete Confirmation?",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Delete",
    closeOnConfirm: false
  },
  function() {
    $.ajax({
        type: "post",
        url: "url",
        data: "data",
        success: function(data) {}
      })
      .done(function(data) {
        swal("Deleted!", "Data successfully Deleted!", "success");
      })
      .error(function(data) {
        swal("Oops", "We couldn't connect to the server!", "error");
      });
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.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">