代码之家  ›  专栏  ›  技术社区  ›  Jimmy

对json错误响应重新运行AJAX success函数

  •  1
  • Jimmy  · 技术社区  · 7 年前

    我有一个ajax调用,用于查询报表队列,然后使用该ID再次查询报表并返回JSON。此代码适用于:

    $(document).ready(function(){
        $("#r2").click(function(){
            $('#loading').show();
            $.ajax({
            url: "report.php", 
            dataType: 'json',
            data: { 
                type: 'queue', 
                ref: 2
            },
            success: function(result){
                console.log(result.reportID); 
                setTimeout(function(){
                console.log("Go"); 
                $.ajax({
                  url: "report.php", 
                  dataType: 'json',
                  data: { 
                  type: 'get', 
                  ref: result.reportID
                },
                success: function(result){ 
                    console.log(result); 
                    $('#loading').hide();
                    $('#output2').html(result.report.totals);
                }
                });
                },1000);
            }});
        });
    });
    

    result.report.totals

    {error: "report_not_ready", error_description: "Report not ready", error_uri: null}
    

    所以,我想让它再次尝试这段代码,得到相同的结果。reportID:

    success: function(result){
        console.log(result.reportID); 
        setTimeout(function(){
        console.log("Go"); 
        $.ajax({
          url: "report.php", 
          dataType: 'json',
          data: { 
          type: 'get', 
          ref: result.reportID
        },
        success: function(result){ 
            console.log(result); 
            $('#loading').hide();
            $('#output2').html(result.report.totals);
        }
        });
        },1000);
    }});
    

    我的尝试如下:

    success: function(result){ 
        if (result.report.error == "report_not_ready") {
        // RERUN THE SUCCESS FUNCTION
        }
        // OTHERWISE OUTPUT THE TOTAL
        $('#output2').html(result.report.totals);
    }
    

    2 回复  |  直到 7 年前
        1
  •  2
  •   Hary    7 年前

    $("#r2").click(function(){
    
    getReport(2, 'queue')
    
    });
    
    function getReport(refId, type)
    {
       $.ajax({
            url: "report.php", 
            dataType: 'json',
            data: { 
                type: type, 
                ref: refId
            },
            success: function(result){
              
               if (refId == 2)
               {
                   getReport(result.reportID, 'get');
               }
               else if(result.report.error == "report_not_ready") 
               {
                   getReport(refId, 'get');
               }
               else
               {
                  $('#output2').html(result.report.totals);
               }
             }
        });
    }
        2
  •  0
  •   GYaN user7305435    7 年前

    如果您的成功结果是JSON,那么在使用之前在数组中对其进行解码。

    就像下面一样

    success: function(result){ 
        resultArray = $.parseJson(result); // Like this
        if (resultArray.report.error == "report_not_ready") {
        // RERUN THE SUCCESS FUNCTION
        }
        // OTHERWISE OUTPUT THE TOTAL
        $('#output2').html(resultArray.report.totals);
    }