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

631
Views
jQuery: manejar errores en getJSON ()?

¿Cómo manejo 500 errores cuando uso getJSON de jQuery?

Ha habido un par de preguntas sobre el manejo de errores con getJSON() y JSONP , pero no estoy trabajando con JSONP, solo con JSON ordinario.

Otra respuesta sugiere usar .ajaxSetup() antes de llamar a getJSON() , así que probé esto:

 $.ajaxSetup({ "error":function() { alert('Error!'); }}); $.getJSON('/book_results/', function(data) { # etc

Pero encuentro que la alerta siempre se activa, incluso cuando el resultado está bien formado.

¿Algunas ideas?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

El método getJSON no devuelve errores de forma nativa, pero puede sumergirse en el objeto xhr que se devuelve como parámetro en la devolución de llamada.

El método getJSON es una función abreviada para jQuery.ajax . Usando jQuery.ajax puede lograr fácilmente el manejo de errores:

 $.ajax({ url: 'http://127.0.0.1/path/application.json', dataType: 'json', success: function( data ) { alert( "SUCCESS: " + data ); }, error: function( data ) { alert( "ERROR: " + data ); } });
over 4 years ago · Santiago Trujillo Report

0

Si está utilizando jquery versión 1.5 o superior, puede usar los nuevos métodos .success(function), .error(function) y .complete(function)

Ejemplo de http://api.jquery.com/jQuery.get/

 // Assign handlers immediately after making the request, // and remember the jqxhr object for this request var jqxhr = $.get("example.php", function() { alert("success"); }) .success(function() { alert("second success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); }); // perform other work here ... // Set another completion function for the request above jqxhr.complete(function(){ alert("second complete"); });

Funciona perfecto para mí. espero que esto ayude

over 4 years ago · Santiago Trujillo Report

0

puede verlo en jquery api getJSON: http://api.jquery.com/jQuery.getJSON/

 $.getJSON(url).done(function(data){ $("#content").append(data.info); }) .fail(function(jqxhr){ alert(jqxhr.responseText); });

// jquery1.5+ la devolución de llamada fallida se activará cuando el texto no sea la cadena json correcta o cualquier otra solución fallida

over 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!