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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10907733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:56:22  来源:igfitidea点击:

Handle 500 errors in JSON (jQuery)

jqueryajaxjson

提问by Fraser

This JSON request:

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

In certain circumstances will bring back a 500 error in the form of:

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

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

This however is still useful to me and I need to be able to be able to handle this correctly.

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

For some reason though, when this 500 error is returned, my error function is not called and I just get a "NetworkError: 500 Internal Server Error" error in firebug.

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

How can I handle this?

我该如何处理?

回答by Prasenjit Kumar Nag

Did you try statuscodecallback like

你试过statuscode回调吗

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

回答by emispowder

Check out the jqXHR Objectdocs. You could use the fail method to capture any errors.

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

Something like the following for your case:

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

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

I would also look into passing a json data string via post instead of attaching query variables:

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

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

回答by Mladen Janjetovic

If you are using POST you can use something like this:

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

回答by Vince V.

I think you could catch it by adding this:

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

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

回答by Fraser

I removed the dataType:json from the ajax call and I was able to catch the error. In these situations I do not need the content of the returned jSON luckily; only to know that there is an error being returned so this will suffice for now. Firebug still has a hissy fit but I am at least able to perform some actions when there is an error

我从 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');
            }
        })