【问题标题】:Handle 500 errors in JSON (jQuery)在 JSON (jQuery) 中处理 500 个错误
【发布时间】:2012-06-06 03:17:03
【问题描述】:

这个 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”错误。

我该如何处理?

【问题讨论】:

  • Firebug 是否在 jQuery 看到它之前就抓住了错误并停止了事情?!
  • 不是,不是。我试过打开和关闭萤火虫
  • 你为什么有async: false?来自 jQuery 文档:“从 jQuery 1.8 开始,不推荐使用 async: false。”
  • 我正在从 $.each 中执行 ajax 调用,这会导致各种问题。从那以后,我将其改造成更好的方法,并且尚未删除 async:false
  • 试试不带async: false。只需删除它;默认为true。可能是由于平台或浏览器错误,调用error 函数的代码不适用于async: false

标签: jquery ajax json


【解决方案1】:

你有没有尝试statuscode回调之类的

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

【讨论】:

  • 文斯也推荐了这个。我试了一下,但我仍然收到错误,并且 jQuery 没有进入函数。
【解决方案2】:

如果您使用的是 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  
        }
    });

【讨论】:

    【解决方案3】:

    查看jqXHR Object 文档。您可以使用 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}))
    

    【讨论】:

      【解决方案4】:

      我想你可以通过添加这个来捕捉它:

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

      【讨论】:

      • 不幸的是,在 Firebug 中仍然给我同样的 500 错误并且没有进入函数
      【解决方案5】:

      我从 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');
                  }
              })
      

      【讨论】:

      • 这是适合您的解决方案,但不是我们需要 JSON 时的解决方案。有人有真正的解决方案吗?
      猜你喜欢
      • 2019-08-20
      • 2019-03-24
      • 1970-01-01
      • 2012-06-11
      • 2023-03-19
      • 1970-01-01
      • 2018-01-29
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多