Callback is executed in the parameter

0

function requestAjax(url, metodo, data, successCallback) {
    $.ajax({
        url: url,
        type: metodo,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(data),
        statusCode: {
            200: () => {
                successCallback();
            }
        }
    });
}

function adicionarNovaCategoria() {
    if ($("#nova-categoria-input").val() !== '') {
        var categoria = {
            nomeCategoria: $("#nova-categoria-input").val()
        };

        requestAjax("/api/categorias-componente", "POST", categoria, function () {
            showAlertaVerde("Item adicionado!");
            getTodasCategorias();
            $("#nova-categoria-input").val('');
        });

    } else {
        showNomeInvalido();
    }
}

The function that I pass as callback on the second addNewCategory function is performed before entering the requestAjax function.

    
asked by anonymous 10.07.2018 / 01:13

1 answer

2

It is necessary to set ajax to asynchronous false.

async: "false"

    
10.07.2018 / 01:33