Handle 500 errors in JSON (jQuery)

前端 未结 5 1251
孤城傲影
孤城傲影 2020-12-28 14:35

This JSON request:

$.ajax({
    url:jSONurl+\'?orderID=\'+thisOrderID+\'&variationID=\'+thisVariationID+\'&quantity=\'+thisQuantity+\'&callback=?         


        
相关标签:
5条回答
  • 2020-12-28 15:16

    Did you try statuscode callback like

     $.ajax({
        statusCode: {
            500: function() {
              alert("Script exhausted");
            }
          }
       });
    
    0 讨论(0)
  • 2020-12-28 15:23

    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');
        }
    })
    
    0 讨论(0)
  • 2020-12-28 15:24

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

    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(jSONurl, $.toJSON({orderID: thisOrderID, variationID: thisVariationID, quantity: thisQuantity, callback: false}))
    
    0 讨论(0)
  • 2020-12-28 15:38

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

    $.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  
            }
        });
    
    0 讨论(0)
  • 2020-12-28 15:41

    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');
                }
            })
    
    0 讨论(0)
提交回复
热议问题