Display an error if ajaxsource has an error

Display an error if ajaxsource has an error

gorristergorrister Posts: 3Questions: 0Answers: 0
edited September 2013 in General
I'm looking to display a alert if my ajaxsource comes up with an error while trying to create its json - this can happen for several reasons. The fnServerParams passed in may be invalid - such as when I'm passing in an id into a database table and that id doesn't exist. I want to tell the user that the data they entered is invalid.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Currently you have to use fnServerData to provide a custom Ajax call. It will be possible to just add error handling to the default Ajax call in 1.10.

    Allan
  • gorristergorrister Posts: 3Questions: 0Answers: 0
    So, would I do something like:
    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "../examples_support/server_processing.php",
    "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) {
    /* check to see if json contains an error? */
    fnCallback(json)
    } );
    }
    } );
    } );
    [/code]
    And I would actually create a different json structure for error conditions and check to see if that error node exists where I have /* check to see... */ ?

    Thanks Allan!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I'd use `$.ajax` since that allows you do define an error function easily, but yes, that's the idea. Or you can have errors reported as JSON - that's the whole point of fnServerData - you control the ajax call - its up to you :-)

    Allan
This discussion has been closed.