Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
jQuery ajax solicita repeticiones cuando ocurre un error

He leído varias publicaciones relacionadas con este tipo de problema, pero todavía no identifico el problema aquí.

Cuando se llama a la siguiente función y recibe una respuesta 200, todo está bien; cuando se encuentra con el 404 se repite el ajax; agregar un tiempo de espera solo limita el período de tiempo durante el cual se realizan las solicitudes de repetición. Tiene que haber una razón simple para esto, pero me está eludiendo...

 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 answers
Answer question

0

Puede usar el enfoque dado a continuación para obtener los datos de su error sin repetición en 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; } });

ACTUALIZAR

No puede devolver la respuesta porque tiene una solicitud asíncrona y la variable de respuesta se devolverá antes de que las solicitudes ajax reales den una respuesta. Por lo tanto, le sugiero que use una función de devolución de llamada en caso de éxito o use una solicitud síncrona.

Entonces, para obtener la respuesta, puede tener una función como esta:

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

O el enfoque de devolución de llamada es:

 $.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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!