Handle 500 errors in JSON (jQuery)

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

This JSON request:

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


        
5条回答
  •  谎友^
    谎友^ (楼主)
    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}))
    

提交回复
热议问题