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

249
Vistas
jQuery ajax requests repeats when error occurs

I've read through several posts related this this kind of issue, but I'm still not identifying the issue here.

When the following function is called and receives a 200 response, all is well; when it encounters 404 the ajax is repeated; adding a timeout only limits the time frame during which the repeat requests are made. There has to be a simple reason for this, but it is eluding me ...

function myFunction(ID) {
    var url = 'http://example.org/' + ID;
    var response;
    $.ajax(url, {
        success: function (responseText) {
            if (responseText !== undefined) {
              response = responseText;
            }
        },
        error: function (xhr, ajaxOptions, errorMsg) {
            if (xhr.status == 404) {
                console.log('404: ' + errorMsg);
            } else if (xhr.status == 401) {
                console.log('401: ' + errorMsg);
            }
        }
    });
    return response;
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use the below given approach to get the data for your error without repetition in AJAX.

$.ajax(url, {
    success: function (responseText) {
        if (responseText !== undefined) {
          response = responseText;
        }
    },
    error: function (xhr) {
        //the status is in xhr.status;
        //the message if any is in xhr.statusText;
    }
});

UPDATE

You cannot return the response because you have an async request and response variable will be returned before the actual ajax requests gives a response. So I suggest You either use a callback function on success ore use a synchronous request.

So to get the response you can have a function like so:

function getResponse() {
    return $.ajax({
        type: "GET",
        url: your_url,
        async: false
    }).responseText;
}

Or the callback approach is:

$.ajax(url, {
    success: function (responseText) {
        if (responseText !== undefined) {
            theCallbackFunction(responseText);
        }
    },
    error: function (xhr) {
        //the status is in xhr.status;
        //the message if any is in xhr.statusText;
    }
});

function theCallbackFunction(data)
{
    //do processing with the ajax response
}
about 4 years ago · Santiago Trujillo 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