2012-06-06 76 views
15

這JSON請求:處理500錯誤

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

在某些情況下會帶回一個500錯誤的形式:

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

然而,這仍然是對我很有用我需要能夠正確處理這個問題。

由於某些原因,當這個500錯誤返回時,我的錯誤函數沒有被調用,我只是在螢火蟲中出現「NetworkError:500 Internal Server Error」錯誤。

我該如何處理?

+0

是Firebug搶在jQuery之前的錯誤和暫停的事情可以甚至看到它?! –

+0

不是,沒有。我試過用螢火蟲打開和關閉 – Fraser

+0

爲什麼你有'async:false'?從jQuery文檔:「從jQuery 1.8開始,不贊成使用async:false。」 –

回答

27

你嘗試statuscode回調像

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

Vince也建議這樣做,我試了一下,但是我仍然收到錯誤,並且jQuery沒有進入函數。 – Fraser

3

我想你可以通過添加此捕獲它:從Ajax調用JSON和我能趕上:

$.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

不幸的是,仍然給我在Firebug 500錯誤,並沒有進入功能 – Fraser

1

我刪除具體的數據類型錯誤。在這些情況下,我不需要幸運地返回jSON的內容;只知道有一個錯誤被返回,所以現在就足夠了。螢火蟲還是有噓聲像配合,但我至少能執行某些操作時出現錯誤

$.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

這是一個適合你的解決方案,但不是我們需要JSON的解決方案。 – jerone

3

退房的jqXHR Object文檔。您可以使用失敗方法來捕獲任何錯誤。

東西像你的情況如下:

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

我也考慮通過郵寄傳遞一個JSON數據字符串,而不是附加查詢變量:

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

如果您正在使用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 
     } 
    });