Sweet Alert on Modal Bootstrap

1

I have the following problem using sweet alert and modal bootstrap. It turns out, I make the requests through ajax and php, but the generated alert always shows me the independent success if the request to the server was successful or not. What I need is that if the request is successful, show the success, otherwise show error. The code that I leave here, is how I have it now. I have tried in many ways and none of them work and I also generated errors, that is why I only write the success of the procedure:

$(document).ready(function(){
$('#edit_nomen').submit(function(e){
    e.preventDefault();
    var datos = $(this).serialize();

    $.ajax({
        type:"POST",
        url: "miarchivo.php",
        data : datos,
        success:function(data){
            $('.modal').modal('hide');
            swal(
                'ALGUN TEXTO',
                'OTRO TEXTO',
                'success'
            );
            $('#respuesta').html(data);
            $('#listado').DataTable().ajax.reload();
        }
    });
});
});

I am using SweetAlert2. It is worth mentioning that this procedure is performed on a modal window. I appreciate any help or guidance that you can give me. Greetings to all.

    
asked by maha1982 05.11.2018 в 04:07
source

1 answer

2

The problem is that for sure you have a try catch on the server so you control the errors and return an answer without problem. My solution is to add a property to the returned Json called response that is a string that takes the value of "OK" if they completed the processes or take the value of the exception.

What I do to know if the answer was correct, I simply make a conditional in the success:

if(data.respuesta == "OK"){

// Peticion correcta
}
else{
// si se controla una excepcion en el servidor
alert(data.respuesta);
}

Keeping in mind that answer is the property that brings my message and data is all Json.

    
answered by 05.11.2018 / 08:38
source