26

この 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 エラーが返されると、エラー関数が呼び出されず、firebug で "NetworkError: 500 Internal Server Error" エラーが発生します。

どうすればこれを処理できますか?

4

5 に答える 5

39

statuscode次のようなコールバックを試しましたか

 $.ajax({
    statusCode: {
        500: function() {
          alert("Script exhausted");
        }
      }
   });
于 2012-06-06T03:55:59.563 に答える
6

jqXHR オブジェクトのドキュメントを確認してください。エラーをキャプチャするには、fail メソッドを使用できます。

あなたの場合は次のようなものです:

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

また、クエリ変数を添付する代わりに、post を介して json データ文字列を渡すことも検討します。

$.post(jSONurl, $.toJSON({orderID: thisOrderID, variationID: thisVariationID, quantity: thisQuantity, callback: false}))
于 2013-04-04T22:07:51.257 に答える
5

これを追加することでそれをキャッチできると思います:

$.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');
    }
})
于 2012-06-06T03:22:02.463 に答える
2

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');
            }
        })
于 2012-06-06T04:12:20.710 に答える