在JSON处理500错误(jQuery的)错误、JSON、jQuery

2023-09-11 22:30:15 作者:诺言的真名叫谎言

这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错误的形式:

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

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.

由于种种原因,虽然,当返回这500错误,我的错误函数不叫,我只是得到一个NetworkError:500内部服务器错误错误萤火

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.

我该如何处理呢?

推荐答案

我删除了数据类型:JSON AJAX请求,我能够捕获错误。在这种情况下,我并不需要返回的JSON幸运的内容;后来才知道有返回一个错误所以这个就足够了现在。萤火虫还是有嘘声配合,但我至少可以执行某些操作时出现错误

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({
            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');
            }
        })