【问题标题】:How to Add Ajax Call with sweet alert如何使用甜蜜警报添加 Ajax 调用
【发布时间】:2015-10-28 10:24:18
【问题描述】:

我的 Ajax 方法是这样的

$.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();
              }


           })

我想显示一个类似这样的甜蜜警报警告弹出窗口

   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 调用,如果他们选择是,那么只有调用应该发生

谁能告诉我如何将 Ajax 方法嵌入到 Sweet Alert 方法中

谢谢

【问题讨论】:

    标签: jquery ajax sweetalert


    【解决方案1】:

    举个简单的例子,我可以向您展示我是如何在我的网站上做到这一点的。我将 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");
              });
            });
           }
    

    因此,只有在您按下确认按钮时才会进行 ajax 调用。我希望这可以帮助您按照需要的方式安排代码。

    【讨论】:

      【解决方案2】:

      这是我在我的网站中使用的代码。

                  swal({
                    title: 'Are you sure?',
                    text: "Are you sure that you want to cancel this order?", 
                    showCancelButton: true,
                    confirmButtonText: 'Confirm',
                    cancelButtonText: 'Cancel',
                    showLoaderOnConfirm: true,
                    preConfirm: function () {
                      return new Promise(function (resolve, reject) {
                          $.ajax({
                               success: function(response) {
                                    resolve(response)
                               },
                               error: function(a, b, c){
                                    reject("error message")
                               }
                          })
                      })
                    },
                    allowOutsideClick: false
                  }).then(function (response) {
      
                      swal({
                        title: 'Success',
                        type: 'success',
                        html: '<p>Thank you</p>',
                        showCancelButton: false,
                        confirmButtonColor: '#3085d6',
                        confirmButtonText: 'Close!',
                        allowOutsideClick: false
                      }).then(function () {
                          window.location = '/';
                      })
      
                  })
      

      在

      preConfirm: function () { return new Promise(function (resolve, reject) {}) }
      

      你必须打电话

      resolve(response)
      

      或

      reject()
      

      在 ajax 响应之后。

      【讨论】:

        【解决方案3】:

        它在站点参考中-

        swal({   title: "Ajax request example",   
            text: "Submit to run ajax request",   
            type: "info",   showCancelButton: true,   
            closeOnConfirm: false,   
            showLoaderOnConfirm: true, 
        }, 
        function(){   
           $.post(url,data,callback)
        });
        

        【讨论】:

          【解决方案4】:

          你可以这样做 获取甜蜜警报的输入并通过 ajax 请求发送

          function change_stock(item_id) {
              swal({
            title: "Edit Stock!!",
            text: "Enter the stock No: you wanded to change",
            type: "input",
            showCancelButton: true,
            closeOnConfirm: false,
            inputPlaceholder: "Write something"
          }, function (inputValue) {
            if (inputValue === false) return false;
            if (inputValue === "") {
              swal.showInputError("You need to write something!");
              return false
            }
            setTimeout(function () { 
              $.ajax( 
                              {  
                                  type: "POST",
                                  url: "edit_stock.php",
                                  data: { stock_no : inputValue,itemid:item_id },
                                  success: function(){
          							swal({ title: "Updated!", text: "Stock No: has Changed", type: "success",}, 
          			                 function () { location.reload(true); });
                                  }
          						
          						
                              }
                      );
                    
            }, 2000); 
          }
          
          );
                     	
          }

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-17
            • 1970-01-01
            • 1970-01-01
            • 2023-03-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多