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

Fra*_*ser 21 ajax jquery json

这个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');
    }
})
Run Code Online (Sandbox Code Playgroud)

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

jQuery17205593111887289146_1338951277057({"message":"Availability exhausted","status":500});
Run Code Online (Sandbox Code Playgroud)

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

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

我怎么处理这个?

Pra*_*Nag 32

你有没有试过statuscode回调?

 $.ajax({
    statusCode: {
        500: function() {
          alert("Script exhausted");
        }
      }
   });
Run Code Online (Sandbox Code Playgroud)

  • 文斯也推荐了这个.我试了一下,但我仍然得到错误,jQuery没有进入该功能. (3认同)

Mla*_*vic 9

如果您正在使用 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  
        }
    });
Run Code Online (Sandbox Code Playgroud)


Vin*_* V. 5

我认为您可以通过添加以下内容来捕捉它:

$.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');
    }
})
Run Code Online (Sandbox Code Playgroud)


emi*_*der 5

查看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);
      });
Run Code Online (Sandbox Code Playgroud)

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

$.post(jSONurl, $.toJSON({orderID: thisOrderID, variationID: thisVariationID, quantity: thisQuantity, callback: false}))
Run Code Online (Sandbox Code Playgroud)