Failed to redirect in a promise

1

Good morning.

I'm doing a script in which ajax performed a process of insertion in BD depending on what returns this function ajax I need to reload the same page or clean the form, if everything goes well continue with another function ajax in the which redirects to another page.

My problem is that regardless of the result of the first function it always redirects to the address of the second function and I need that if the condition on the first ajax call is not fulfilled recharge the same page but in this case it continues with the execution.

I am using "promises". Any idea, suggestion to do this or some way that is better?

I leave my code so you can see what I'm trying.

$(document).ready(function () {
$("#btnRegistroExamen").click(function (e) {
    e.preventDefault();
    var id = $("#Userid").text(),
        FechaExamen = $("#Fechas").val(),
        FolioP = $("#FolioP").val(),
        FechaPago = $("#fechaP").val(),
        name = $("#nombre").val(),
        lastName = $("#Apellidos").val(),
        semestre = $("#Semestre").val(),
        carrera = $("#Carrera").val(),
        email = $("#email").val(),
        nControl = $("#Nctrl").val();

    var datos = {
        "id": id,
        "FechaExamen": FechaExamen,
        "FolioP": FolioP,
        "FechaPago": FechaPago
    }
    var completeData = {
        "id": id,
        "FechaExamen": FechaExamen,
        "FolioP": FolioP,
        "FechaPago": FechaPago,
        "nombre": name,
        "apellido": lastName,
        "semestre": semestre,
        "carrera": carrera,
        "email": email,
        "nControl": nControl
    };


    var Registro = $.ajax({
        url: "php/RegistrarExamen.php",
        data: datos,
        method: "POST",
        beforeSend: function () {
            console.log("Enviando datos");
        },
        success: function (data) {
            console.log("Excito en envio de info...");
            if (!data) {
                console.log(data);
                alert(data);
                location.reload();
            }
        },
        error: function () {
            console.log("Fallo en envio");
        }
    });

    Registro.promise().done(function () {
        //Generacion de QR
        $.ajax({
            url: "php/qrGenerator.php",
            data: completeData,
            method: "POST",
            beforeSend: console.log("Enviando QR"),
            success: function (data) {
                if (data) {
                    location.href = 'RExamenExito.php?id=${id}';
                } else {
                    console.log("Error en generacion de QR");
                }
            },
            error: function () {
                console.log("Error en QR");
            }
        });
    });
});
});

Thank you in advance for your help and comments.

Greetings.

    
asked by lbarajas 03.01.2019 в 07:51
source

1 answer

1

according to the comment I understand that your condition is! data, that if it is null or empty, reload the page. You comment if making nested Ajax calls can cause conflict, so that does not happen that the callback success tells us that the first ajax call has ended so you can do the following.

var Registro = $.ajax({
    url: "php/RegistrarExamen.php",
    data: datos,
    method: "POST",
    beforeSend: function () {
        console.log("Enviando datos");
    },
    success: function (data) {
        console.log("Excito en envio de info...");
        if (!data) {
            console.log(data);
            alert(data);
            location.reload();
        }
        else{
            $.ajax({
                        url: "php/qrGenerator.php",
                        data: completeData,
                        method: "POST",
                        beforeSend: console.log("Enviando QR"),
                        success: function (data) {
                            if (data) {
                                location.href = 'RExamenExito.php?id=${id}';
                            } else {
                                console.log("Error en generacion de QR");
                            }
                        },
                        error: function () {
                            console.log("Error en QR");
                        }
                    });
        }
    },
    error: function () {
        console.log("Fallo en envio");
    }
});

It seems to me that this should work for you, but the truth is that the code is not readable, so it is better to make the second call in another function. Example:

var Registro = $.ajax({
    url: "php/RegistrarExamen.php",
    data: datos,
    method: "POST",
    beforeSend: function () {
        console.log("Enviando datos");
    },
    success: function (data) {
        console.log("Excito en envio de info...");
        if (!data) {
            console.log(data);
            alert(data);
            location.reload();
        }
        else{
            ajaxGenerarQR(completeData);
        }
    },
    error: function () {
        console.log("Fallo en envio");
    }
});

function ajaxGenerarQR(completeData)
{
    $.ajax({
        url: "php/qrGenerator.php",
        data: completeData,
        method: "POST",
        beforeSend: console.log("Enviando QR"),
        success: function (data) {
            if (data) {
                location.href = 'RExamenExito.php?id=${id}';
            } else {
                console.log("Error en generacion de QR");
            }
        },
        error: function () {
            console.log("Error en QR");
        }
    }); 
}

I hope it works for you

    
answered by 03.01.2019 / 09:32
source