Is there a way to handle the status code "202 Accepted"?

Is there a way to handle the status code "202 Accepted"?

robertrishrobertrish Posts: 6Questions: 1Answers: 0

Reference: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success

I'm getting a 202 status back, and I would like to keep reloading the same table until the status comes back as success (200).

Is there a way to do the following:
- Keep re-sending ajax call until the status code changes
- Not reload the table each time the ajax call is sent
- Keep the "Processing..." message visible until the status has changed

Thank you for your help.

Regards,
Rob

This question has an accepted answers - jump to answer

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    I have no idea.

    OK, now that we have that out of the way, I have questions that might point int he right direction. What else comes back from the 202 message? If the server returns 202, what key do you have to return to the server with a followup request and get the matching results?

  • robertrishrobertrish Posts: 6Questions: 1Answers: 0

    The way the table is currently working is this:

    The request is sent with the default table parameters, the data in the table is generated on a remote server. It takes some time to generate the data, so the server returns the 202 and whatever message we want; which is in this case "Data not ready".

    The solution that I've found is the following:

    When the status is returned as 202, I'm stopping the datatable load and calling a recursive function that will keep checking for the server status:

    preDrawCallback: function(response) {
        if (response.jqXHR.status === 202) {
            preloadRemoteData(response, tableURL, loadAdsDataTable);
            return false;
        }
    },
    

    The recursive function:

    function preloadRemoteData(tableSettings, tableURL, functionToRun) {
        var ajaxFunction = function() {
            // Show the processing message for the current table until 200 response is returned
            var dataTableId = $(tableSettings.oInstance).parent().attr('id');
            $('#' + dataTableId.replace('_wrapper', '_processing')).show();
            var xhr = $.ajax({
                type: 'POST',
                url: tableURL,
                data: tableSettings.oAjaxData
            }).success(function (data, textStatus, jqXHR) {
                if (jqXHR.status !== 202) {
                    functionToRun(); // Reload the table when the status is no longer 202
                } else {
                    ajaxFunction();
                }
            });
        }
        ajaxFunction();
    }
    

    Table reload function:

    function loadAdsDataTable() {
         campaignAdsTable.ajax.reload(null, false);
    }
    
  • ThomDThomD Posts: 334Questions: 11Answers: 43

    that's a cool approach, but doesn't it tend to take over the browser while it recurses? If I had to deal that that, I'd probably use SetInterval().

  • robertrishrobertrish Posts: 6Questions: 1Answers: 0

    That is exactly what I was doing, but we already have a delay on the server, which also allows me to abort the AJAX call in progress if i need to re-sort the table by another column (which calls a different tableURL). There's nothing else to do on the page while the stats are loading, so the taking over the browser is not an issue at this point.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    That looks like a good approach to me. The other option would be to use ajax as a function and make your own Ajax calls - continue to do so (in a simple recursive loop as you have) until a 200 is returned and then finally trigger the success callback.

    One more option is to do the Ajax externally to DataTables and use rows.add() when the data is ready.

    Regards,
    Allan

  • robertrishrobertrish Posts: 6Questions: 1Answers: 0

    That's a great idea. Thanks!

This discussion has been closed.