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
Function not returning the value inside variable after AJAX call

How do I get my function saveAttempt() to return score? The alert() displays the correct number in variable score, but the returned value is [Object Object]. Using toString(score) produces [object undefined].

What am I doing wrong?

function saveAttempt(uid,cid,code,success) {
    var score = $(document).ready(function(){   
                $.ajax({
                    type: "POST",
                    url: {$js_url} + '/wp-content/plugins/CSUKCoderTestEnviro/save_attempt.php',
                    data: {"uid" : uid, "cid" : cid, "code" : code, "success" : success},
                    success: function(data){
                        grabResponse = data;
                        grabResponse = JSON.parse(grabResponse);
                        let corr_attempt = 0;
                        let incorr_attempt = 0;
                        for(let i = 0; i < grabResponse.length; i++) {
                            if (grabResponse[i] == 0) {
                                incorr_attempt = incorr_attempt + 1;
                            } else {
                                corr_attempt = 1;
                            }
                        }
                        if (corr_attempt == 1) {
                            var score = 100 - incorr_attempt;
                        } else {
                            var score = 0;
                        }
                        alert(score);
                        }
                        });
                return score;
                }); 
            return score;
            }
about 4 years ago · Juan Pablo Isaza
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