Sweet Alert on Modal Bootstrap


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:

    var datos = $(this).serialize();

        url: "miarchivo.php",
        data : datos,
                'ALGUN TEXTO',
                'OTRO TEXTO',

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

1 answer


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
// si se controla una excepcion en el servidor

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

answered by 05.11.2018 / 08:38