How to Add Ajax Call with sweet alert

前端 未结 4 1197
后悔当初
后悔当初 2020-12-18 00:08

My Ajax method looks like this

$.post(url,{
            ajax_call:\"putDonation\",
            addresse:addresse,
            phone:phone,
            email         


        
相关标签:
4条回答
  • 2020-12-18 00:23

    Its there in the site reference-

    swal({   title: "Ajax request example",   
        text: "Submit to run ajax request",   
        type: "info",   showCancelButton: true,   
        closeOnConfirm: false,   
        showLoaderOnConfirm: true, 
    }, 
    function(){   
       $.post(url,data,callback)
    });
    
    0 讨论(0)
  • 2020-12-18 00:25

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

    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.

    0 讨论(0)
  • 2020-12-18 00:28

    You can do it like this Take a input for sweet alert and send it by ajax request

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

    0 讨论(0)
  • 2020-12-18 00:30

    This is my code use in my site.

                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 = '/';
                    })
    
                })
    

    In

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

    You must call

    resolve(response)
    

    or

    reject()
    

    after ajax responses.

    0 讨论(0)
提交回复
热议问题