Data format for Ajax requests

Data format for Ajax requests

spidogrspidogr Posts: 23Questions: 9Answers: 0
edited June 2021 in DataTables 1.10

I am considering using Ajax for medium-sized tables, but it is not clear to me in the linked example above what sort of format the data in the external txt file must be in. I can see in that example a call to /ajax/data/arrays.txt but what format should arrays.txt be in? Could I use a tab delimited file? In such a case, would I do it with header rows for each column name?

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The format is shown in the Ajax tab of the example you linked to. Datatables supports JSON formatted data. See the Ajax manual for more info.

    Kevin

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited June 2021

    The data is visible under the Ajax tab on the page you linked to.

    Could I use a tab delimited file?

    No. json is expected.

  • spidogrspidogr Posts: 23Questions: 9Answers: 0

    Right thanks.
    I tested and it seems to work. However, when I try to limit elements displayed by adding "dom": 'frtip' then it does not work. Do I need different syntax here? Nor if I change the arrays.txt to arrays.json.

    $(document).ready( function () {
      $('#example').dataTable( {
        "dom": 'frtip'
        "ajax": 'arrays.txt'
      } );
    } );
    
  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    Look at the browser's console and you will see you have a syntax error. You need a comma between the options, like this:

    $(document).ready( function () {
      $('#example').dataTable( {
        "dom": 'frtip',  // comma goes here
        "ajax": 'arrays.txt'
      } );
    } );
    

    Kevin

  • spidogrspidogr Posts: 23Questions: 9Answers: 0

    Great, thanks!

Sign In or Register to comment.