如何使用甜蜜警报添加Ajax呼叫 [英] How to Add Ajax Call with sweet alert

查看:78
本文介绍了如何使用甜蜜警报添加Ajax呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Ajax方法如下所示

My Ajax method looks like this

$.post(url,{
            ajax_call:"putDonation",
            addresse:addresse,
            phone:phone,
            email:email,
            name:name,
            amount:amount,
            amountinwords:amountinwords
           }).done(function(data){

            console.log(data);

            var arr = [];

            try {
                  var str = "";
                  //convert to json string
                      arr = $.parseJSON(data); //convert to javascript array
                      $.each(arr,function(key,value){
                        str +="<li>"+value+"</li>";
                    });
                       alert(str);
                       $(".alert-danger").show();
                       $("#error").html(str);

              } catch (e) {
                  swal("Jay Gayatri !", 'Donation Sucessful', "success")
                  $("#donation")[0].reset();
              }


           })

我想显示类似这样的甜美警告警告"弹出窗口

I want to show a sweet Alert Warning popup something like this one

   swal({   
                title: "Are you sure ?",   
                text: "You will not be able to recover this imaginary file!",   
                type: "warning",   
                showCancelButton: true,   
                confirmButtonColor: "#DD6B55",   
                confirmButtonText: "Yes, delete it!",   
                closeOnConfirm: false 
              }, 
                function(){   
                  swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
                });

如果他们单击取消",则不应进行ajax调用;如果选择是",则仅应进行调用

And if they click the cancel it should not do the ajax call , If they select yes then only the call should happen

所以任何人都可以告诉我如何将Ajax方法嵌入到Sweet Alert方法中

So can any one tell me how can I embed the Ajax method inside Sweet Alert methods

谢谢

推荐答案

举个简单的例子,我可以向您展示我如何在自己的网站上做到这一点.我将Ajax呼叫置于最甜蜜的警报中.

For a quick example i can show you how i did it on my site. I put the Ajax call inside the sweet alert.

    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");
          });
        });
       }

因此,仅当您按下确认按钮时,才会进行ajax调用.我希望这可以帮助您按照需要的方式排列代码.

So the ajax call only gets made if you press the confirm button. I hope this can help you to arrange your code the way you need it.

这篇关于如何使用甜蜜警报添加Ajax呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆