How to use ajax inside a Sweetalert?

Asked

Viewed 377 times

1

I tried this way, but I believe it is not the right way. I am 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");   
        } 
    });

   }    
  • I don’t know if that’s the problem, but it seems to me if (isConfirm) { {function(id) there is a syntax error. Maybe this

  • Returned some error in the console?

1 answer

2


I don’t know exactly how this sweetalert works, but I rewrote the function correcting some syntax errors and changing a little ajax to have a successful callback or failure:

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 helped, until.

  • Thanks Luis, really it was syntax error :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.