Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
Why is this AJAX/POST taking precedence over the entire block of code

I am sorry, but I cannot for the life of me figure this out.

function submit_order_form() {

    if (typeof storedFiles !== 'undefined') {

            jQuery('.overlay-loading').css('visibility', 'visible')
            return false;
            var formData = new FormData();
            for (var key in storedFiles) {
                    formData.append(key, storedFiles[key]);
            }
            formData.append("rep", $('#sales_rep').val() );
            $.ajax({
                    url: '/send_invoice.php',
                    type: 'POST',
                    data: formData,
                    dataType: "JSON",
                    processData: false,
                    contentType: false,
                    cache: false,
                    async: false,
                    success: function (response) {
                            jQuery('#new_files_ajax').empty();
                            jQuery('#new_files_ajax').text(response.message);
                            jQuery('.overlay-loading').css('visibility', 'hidden')

                    },
                    error: function(error) {
                            jQuery('.overlay-loading').css('visibility', 'hidden')
                            console.log(error);
                    }
            });
    }

}

I want the

            jQuery('.overlay-loading').css('visibility', 'visible')

to run before the ajax call, which it does not, the ajax call just goes through fine but that visibility css change never happens until right at the end for 1 second and then goes back again because its called in the success function. If I return false right before the ajax call it works as expected. What in the world am I missing here? Its not async it should be waiting for that visibility call to take place.

Completely stumped.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Are you by any chance calling this function from a button embedded on your HTML markup page inside of the <form> tag? If so, you'll need to pass the event object to the function so you can call event.preventDefault() which will prevent the page from reloading on when the request goes through.

By default when a form is submitted, the browser will automatically refresh, so you're visibility toggle will never work properly since the page is reloaded so the overlay would be rendered back to it's original state of being invisible.

Also just as a side note with the FormData call, you can pass the storedFiles object into the constructor rather than looping through the keys of storedFiles and appending each property to the FormData you created as you're not renaming keys in the form data to pass to the API endpoint.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I can't be sure because I don't have any other code, but you can see how this works.

function submit_order_form() {

    if (typeof storedFiles !== 'undefined') {

        jQuery('.overlay-loading').css('visibility', 'visible')
        
        setTimeout(function(){
            var formData = new FormData();
            for (var key in storedFiles) {
                    formData.append(key, storedFiles[key]);
            }
            formData.append("rep", $('#sales_rep').val() );
            $.ajax({
                    url: '/send_invoice.php',
                    type: 'POST',
                    data: formData,
                    dataType: "JSON",
                    processData: false,
                    contentType: false,
                    cache: false,
                    async: false,
                    success: function (response) {
                            jQuery('#new_files_ajax').empty();
                            jQuery('#new_files_ajax').text(response.message);
                            jQuery('.overlay-loading').css('visibility', 'hidden')

                    },
                    error: function(error) {
                            jQuery('.overlay-loading').css('visibility', 'hidden')
                            console.log(error);
                    }
            });
        }, 0);
        return false; // <=========
    }
}

Of course, this is temporary and in practice it is recommended to fix the entire code with asynchronous calls. (async: true)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda