26

这个 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');
    }
})

在某些情况下,会以以下形式返回 500 错误:

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

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

但是由于某种原因,当返回这个 500 错误时,我的错误函数没有被调用,我只是在 firebug 中得到一个“NetworkError:500 Internal Server Error”错误。

我该如何处理?

4

5 回答 5

39

您是否尝试statuscode过像这样的回调

 $.ajax({
    statusCode: {
        500: function() {
          alert("Script exhausted");
        }
      }
   });
于 2012-06-06T03:55:59.563 回答
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  
        }
    });
于 2016-02-18T16:27:22.710 回答
6

查看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}))
于 2013-04-04T22:07:51.257 回答
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');
    }
})
于 2012-06-06T03:22:02.463 回答
2

我从 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');
            }
        })
于 2012-06-06T04:12:20.710 回答