programing

JSON(jQuery)에서 500개의 오류 처리

starjava 2023. 8. 15. 09:40
반응형

JSON(jQuery)에서 500개의 오류 처리

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 오류가 반환되면 오류 함수가 호출되지 않고 방화벽에서 "NetworkError: 500 Internal Server Error" 오류가 발생합니다.

어떻게 하면 좋을까요?

해보셨습니까statuscode와 같은 콜백.

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

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

jqXHR 오브젝트 문서를 확인합니다.실패 방법을 사용하여 오류를 캡처할 수 있습니다.

귀하의 경우 다음과 같은 것이 경우:

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

나는 또한 쿼리 변수를 첨부하는 대신 포스트를 통해 json 데이터 문자열을 전달하는 것을 검토할 것입니다.

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

다음과 같이 추가하면 이해할 수 있을 것 같습니다.

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

나는 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');
            }
        })

언급URL : https://stackoverflow.com/questions/10907733/handle-500-errors-in-json-jquery

반응형