How to use ajax inside a SweetAlert?

1

I tried that, but I do not think it's the right way. I'm actually sure, because the code does not work.

function removeCliente(id)
  {

   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!",
   cancelButtonText: "No, cancel plx!",
   closeOnConfirm: false,
   closeOnCancel: false
      },
    function(isConfirm){
       if (isConfirm) {
             {function(id)
                $.ajax({
            type: "POST",
            url:'removeClientes.php',
            data: {
                'idexclusao': id

            });
             },
            swal("Deleted!",
             "Your imaginary file has been deleted.", "success");

             window.location.href = 'clientes.php';   
        } 
        else 
        { 
            swal("Cancelled", "Your imaginary file is safe :)", "error");   
        } 
    });

   }    
    
asked by anonymous 09.07.2015 / 18:37

1 answer

2

I do not know exactly how this sweetalert works, but I've rewritten the function by correcting some syntax errors and slightly changing ajax to have a successful or failed callback:

function(isConfirm){
       if (isConfirm) {

            $.ajax({
                type: "POST",
                url:'removeClientes.php',
                data: ({'idexclusao': id})
            }).done(function(data) {
                swal("Deleted!", "Your imaginary file has been deleted.", "success");
                window.location.href = 'clientes.php';
            }).fail(function(data) {
                //código em caso de erro
                swal("Cancelled", "Your imaginary file is with some problem :(", "error");   
            });

        } 
        else 
        { 
            swal("Cancelled", "Your imaginary file is safe :)", "error");   
        } 
}

I hope I have helped, until.

    
10.07.2015 / 16:02