Jimmy
Jimmy

Reputation: 12487

Rerun AJAX success function on json error response

I have a ajax call which is meant to query a report queue and then use that ID to query the report again and return JSON. This code works:

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

Sometimes though, the report isn't ready, in which case, we get this response in JSON instead of the result.report.totals

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

So, what I am after is for it to try this bit of code again with the same result.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);
}});

My attempt at this is as follows:

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

How can I ask it to loop back through the success function to retry the querying the report?

Upvotes: 1

Views: 211

Answers (2)

Hary
Hary

Reputation: 5818

Firstly, here you're not repeating your code and just replacing it with parameters. Also, it allows calling recursively whenever needed.

$("#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);
           }
         }
    });
}

Upvotes: 2

Gyandeep Sharma
Gyandeep Sharma

Reputation: 2327

If your success result is in JSON, then decode it in the array before use.

Like below

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

Upvotes: 0

Related Questions