处理JSON中的500个错误(jQuery)

这个JSON请求:

$.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'); } }) 

在某些情况下,将以以下forms带回500错误:

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

然而,这对我来说仍然有用,我需要能够正确处理这个问题。

但由于某种原因,当返回此500错误时,我的错误函数未被调用,我只是在firebug中出现“NetworkError:500 Internal Server Error”错误。

我怎么处理这个?

您是否尝试过使用状态码回调?

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

查看jqXHR对象文档。 您可以使用fail方法捕获任何错误。

对于您的情况,类似于以下内容:

 $.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); }); 

我还将研究通过post传递json数据字符串而不是附加查询变量:

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

我想你可以通过添加这个来抓住它:

 $.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'); } }) 

如果您使用的是POST,可以使用以下内容:

 $.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 } }); 

我从ajax调用中删除了dataType:json,我能够捕获错误。 在这些情况下,我不需要返回的jSON的内容; 只知道返回有错误,所以现在就足够了。 Firebug仍然适合他,但我至少能够在出现错误时执行某些操作

 $.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'); } })