"Processing" notice and AJAX error handling

"Processing" notice and AJAX error handling

DaveBurnsDaveBurns Posts: 17Questions: 0Answers: 0
edited November 2011 in DataTables 1.8
If the Ajax call to get data from the server fails and bProcessing is set to true, the box with "Processing..." remains. Is this expected behavior? Is there an official way to dismiss it? (Obviously I can find the element and set it to hidden but that doesn't feel "clean").

Replies

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    Yes - there has been an error that shouldn't occur, so DataTables remains in that state (mainly to indicate to the developer that there has been an error that needs to be fixed with the JSON source).

    The "fix" in DataTables would be to modify the $ajax call's error handler to hide the processing element, but I'm reluctant to do that since it will make it less likely the developer will see the problem.

    Allan
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    is there a preferred way to hide the processing message? there was an occasion I needed to clear it and used jQuery .hide() on the div container.
  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    The internal function _fnProcessingDisplay (exposed in oApi) is the preferred way of doing it - although currently all it really does is set display:none on the element - rather like what hide() does.

    Allan
  • DaveBurnsDaveBurns Posts: 17Questions: 0Answers: 0
    Allan - Just so future readers don't misunderstand, this can come up with more than just malformed JSON. If the server returns an HTTP result code such as a 404 or 500, this occurs too. I think (?) the issue is that DataTables sets no default error handler for the Ajax call.

    In the end, I added a fnServerData handler and in the $.ajax call, I added:

    [code]"error": handleAjaxError[/code]

    In the function handleAjaxError, among other cleanup, I called

    [code]table.fnProcessingIndicator( false );[/code]

    db
  • trishdevtrishdev Posts: 13Questions: 1Answers: 0
    DaveBurns -- can you show the entire fnServerData handler and handleAjaxError code? I have been looking for ajax error handling / load handling with processing indicator.. thanks...
  • DaveBurnsDaveBurns Posts: 17Questions: 0Answers: 0
    @trishdev - It's a little tough to remember because I've since incorporated the pipelining code into my $.ajax call but I think this is what you want to add to your datatables options:

    [code]
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback,
    "timeout": 15000, // optional if you want to handle timeouts (which you should)
    "error": handleAjaxError // this sets up jQuery to give me errors
    } );
    },
    [/code]

    And then my error handling function:

    [code]
    function handleAjaxError( xhr, textStatus, error ) {
    if ( textStatus === 'timeout' ) {
    alert( 'The server took too long to send the data.' );
    }
    else {
    alert( 'An error occurred on the server. Please try again in a minute.' );
    }
    myDataTable.fnProcessingIndicator( false );
    }
    [/code]

    And then finally, code I got from elsewhere on this site to define that function I'm using to turn off the processing indicator:

    [code]
    jQuery.fn.dataTableExt.oApi.fnProcessingIndicator = function ( oSettings, onoff ) {
    if ( typeof( onoff ) == 'undefined' ) {
    onoff = true;
    }
    this.oApi._fnProcessingDisplay( oSettings, onoff );
    };
    [/code]

    Hope this helps.
    db
  • trishdevtrishdev Posts: 13Questions: 1Answers: 0
    Hi DB just wanted to thank you (rather late) for posting your code. I had moved onto other thing but am circling back around and will try it out.. thanks again
  • guerrilaguerrila Posts: 8Questions: 0Answers: 0
    @DaveBurns

    This solution worked perfectly. Thanks so much!
This discussion has been closed.