2012-06-06 8 views
15

Esta solicitud JSON:mango 500 errores en JSON (jQuery)

$.ajax({ 
    url:jSONurl+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&quantity='+thisQuantity+'&callback=?', 
    async: false, 
    type: 'POST', 
    dataType: 'json', 
    success: function(data) { 
     if (data.response == 'success'){ 
      //show the tick. allow the booking to go through 
      $('#loadingSML'+thisVariationID).hide(); 
      $('#tick'+thisVariationID).show(); 
     }else{ 
      //show the cross. Do not allow the booking to be made 
      $('#loadingSML'+thisVariationID).hide(); 
      $('#cross'+thisVariationID).hide(); 
      $('#unableToReserveError').slideDown(); 
      //disable the form 
      $('#OrderForm_OrderForm input').attr('disabled','disabled'); 
     } 
    }, 
    error: function(data){ 
     alert('error'); 
    } 
}) 

En ciertas circunstancias será traer de vuelta un error 500 en forma de:

jQuery17205593111887289146_1338951277057({"message":"Availability exhausted","status":500}); 

Sin embargo, esto sigue siendo útil para mí y necesito ser capaz de manejar esto correctamente

Por alguna razón, cuando se devuelve este error 500, no se llama a mi función de error y me aparece un error "NetworkError: 500 Internal Server Error" en firebug.

¿Cómo puedo manejar esto?

+0

Firebug detecta el error y detiene las cosas antes de que jQuery pueda incluso verlo ?! –

+0

No es, no. Lo he intentado con firebug abierto y cerrado – Fraser

+0

¿Por qué tienes 'async: false'? De los documentos de jQuery: "A partir de jQuery 1.8, el uso de async: false está en desuso". –

Respuesta

27

¿Usted intentó statuscode devolución de llamada como

$.ajax({ 
    statusCode: { 
     500: function() { 
      alert("Script exhausted"); 
     } 
     } 
    }); 
+3

Vince también lo recomendó. Lo intenté pero sigo recibiendo el error y jQuery no está ingresando a la función. – Fraser

3

Creo que se podría atraparlo añadiendo esto:

$.ajax({ 
    statusCode: { 
     500: function() { 
     alert("error"); 
     } 
    }, 
    url:jSONurl+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&quantity='+thisQuantity+'&callback=?', 
    async: false, 
    type: 'POST', 
    dataType: 'json', 
    success: function(data) { 
     if (data.response == 'success'){ 
      //show the tick. allow the booking to go through 
      $('#loadingSML'+thisVariationID).hide(); 
      $('#tick'+thisVariationID).show(); 
     }else{ 
      //show the cross. Do not allow the booking to be made 
      $('#loadingSML'+thisVariationID).hide(); 
      $('#cross'+thisVariationID).hide(); 
      $('#unableToReserveError').slideDown(); 
      //disable the form 
      $('#OrderForm_OrderForm input').attr('disabled','disabled'); 
     } 
    }, 
    error: function(data){ 
     alert('error'); 
    } 
}) 
+0

Desafortunadamente, todavía me da el mismo error 500 en Firebug y no entra en la función – Fraser

1

me quita el tipo de datos: JSON de la llamada AJAX y yo era capaz de atrapar el error. En estas situaciones, no necesito el contenido del jSON devuelto por suerte; solo para saber que se ha devuelto un error, así que esto será suficiente por ahora. Firebug todavía tiene un berrinche, pero estoy al menos capaz de realizar algunas acciones cuando hay un error

$.ajax({ 
      url:'http://example.com/jsonservice/LiftieWeb/reserve?token=62e52d30e1aa70831c3f09780e8593f8&orderID='+thisOrderID+'&variationID='+reserveList+'&quantity='+thisQuantity+'&callback=?', 
      type: 'POST', 
      success: function(data) { 
       if (data.response == 'Success'){ 
        //show the tick. allow the booking to go through 
        $('#loadingSML'+thisVariationID).hide(); 
        $('#tick'+thisVariationID).show(); 
       }else{ 
        //show the cross. Do not allow the booking to be made 
        $('#loadingSML'+thisVariationID).hide(); 
        $('#cross'+thisVariationID).hide(); 
        $('#unableToReserveError').slideDown(); 
        //disable the form 
        $('#OrderForm_OrderForm input').attr('disabled','disabled'); 
       } 
      }, 
      error: function(data){ 
       alert('error'); 
      } 
     }) 
+5

Esta es una solución para usted, pero no una solución cuando necesitamos JSON. ¿Alguien tiene una solución real? – jerone

3

Mira la jqXHR Object docs. Puede usar el método de falla para capturar cualquier error.

algo como lo siguiente para su caso:

$.post(jSONurl+'?orderID='+thisOrderID+'&variationID='+thisVariationID+'&quantity='+thisQuantity+'&callback=?') 
.done(function(data){ 
     if (data.response == 'success'){ 
      //show the tick. allow the booking to go through 
      $('#loadingSML'+thisVariationID).hide(); 
      $('#tick'+thisVariationID).show(); 
     }else{ 
      //show the cross. Do not allow the booking to be made 
      $('#loadingSML'+thisVariationID).hide(); 
      $('#cross'+thisVariationID).hide(); 
      $('#unableToReserveError').slideDown(); 
      //disable the form 
      $('#OrderForm_OrderForm input').attr('disabled','disabled'); 
     } 
     }, "json") 
.fail(function(jqXHR, textStatus, errorThrown){ 
     alert("Got some error: " + errorThrown); 
     }); 

también me gustaría ver en pasar una cadena de datos JSON a través de correo en lugar de asociar variables de consulta:

$.post(jSONurl, $.toJSON({orderID: thisOrderID, variationID: thisVariationID, quantity: thisQuantity, callback: false})) 
2

Si está utilizando los mensajes que usted puede usar algo como esto:

$.post('account/check-notifications') 
    .done(function(data) { 
     // success function 
    }) 
    .fail(function(jqXHR){ 
     if(jqXHR.status==500 || jqXHR.status==0){ 
      // internal server error or internet connection broke 
     } 
    }); 
Cuestiones relacionadas