How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Load Data in DataTables Using Ajax
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Load Data in DataTables Using Ajax
Web Development

How to Load Data in DataTables Using Ajax

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Load DataTables data via Ajax
SHARE

To load data into DataTables via Ajax, pass an ajax URL and a columns array to $('#table').DataTable({ ... }). The server returns JSON with a top-level data array containing one object per row, and DataTables maps each columns[i].data key to that row’s matching property.

Contents
  • Client-side load (every row, paginate in the browser)
  • The JSON shape
  • PHP example for the endpoint
  • Bare array response — dataSrc: ''
  • Add a custom action column
  • For larger datasets — server-side processing
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with DataTables 2.0. Originally published 2022-11-06, rewritten and updated 2026-05-17.

Client-side load (every row, paginate in the browser)

$(document).ready(function () {
    $('#my-table').DataTable({
        ajax: '/api/employees',
        columns: [
            { data: 'name' },
            { data: 'position' },
            { data: 'office' },
            { data: 'extn' },
            { data: 'start_date' },
            { data: 'salary' },
        ],
    });
});

DataTables fetches the full dataset once and handles paginate/sort/search client-side. Fine for hundreds of rows; switch to server-side processing past that.

DataTables Ajax load — JSON shape, columns mapping, custom render, server-side flag

The JSON shape

{
  "data": [
    {
      "id": 1,
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "id": 2,
      "name": "Garrett Winters",
      "position": "Accountant",
      "salary": "$170,750",
      "start_date": "2011/07/25",
      "office": "Tokyo",
      "extn": "8422"
    }
  ]
}

PHP example for the endpoint

<?php
header('Content-Type: application/json');

$rows = [
    [
        'id' => 1, 'name' => 'Tiger Nixon', 'position' => 'System Architect',
        'salary' => '$320,800', 'start_date' => '2011/04/25',
        'office' => 'Edinburgh', 'extn' => '5421',
    ],
    [
        'id' => 2, 'name' => 'Garrett Winters', 'position' => 'Accountant',
        'salary' => '$170,750', 'start_date' => '2011/07/25',
        'office' => 'Tokyo', 'extn' => '8422',
    ],
];

echo json_encode(['data' => $rows]);

The whole response is wrapped in { "data": [...] } because DataTables reads rows from data by default.

Bare array response — dataSrc: ''

$('#my-table').DataTable({
    ajax: { url: '/api/employees', dataSrc: '' },
    columns: [ /* ... */ ],
});

// Endpoint now returns a bare array:
// [ { id: 1, name: ... }, { id: 2, ... } ]

Useful when you can’t change the server to wrap the array (third-party API, fixed-shape endpoint).

Add a custom action column

columns: [
    { data: 'name' },
    { data: 'position' },
    // ...
    {
        data: null,
        orderable: false,
        searchable: false,
        render: (data, type, row) =>
            `<a href="/employees/${row.id}/edit">Edit</a>`,
    },
],

data: null means “no source field” — the cell content comes from render, which receives the entire row. Mark such columns orderable: false and searchable: false so DataTables doesn’t try to sort or search on the HTML.

For larger datasets — server-side processing

$('#my-table').DataTable({
    serverSide: true,
    processing:  true,
    ajax:        '/api/employees',
    columns:     [ /* ... */ ],
});

With serverSide: true, every interaction sends a fresh request with pagination/sort/search params, and the server returns just the slice. See DataTables server-side pagination for the request/response contract.

Frequently asked questions

Why does my Ajax response need a data wrapper?

DataTables expects the row array under a key by default (the key is configurable via ajax.dataSrc; default is 'data'). The wrapper lets you include other top-level fields too — pagination metadata, server timestamps, debug flags — without DataTables choking on them. If you can’t change the server to wrap the array, set ajax: { url: '...', dataSrc: '' } to read a bare array.

What’s the difference between ajax and serverSide: true?

With just ajax, DataTables fetches every row once and paginates/sorts/searches them in the browser — fine for hundreds of rows. With serverSide: true, every interaction (page change, sort click, search keystroke) sends a fresh Ajax request and your server returns just the slice for that view. Use server-side when the dataset is too large to ship at once.

Why don’t my columns line up if my JSON has more fields than the table?

DataTables maps each columns[i].data entry to a property on the row object. Extra fields in JSON are ignored — they don’t appear because there’s no column for them. Fields missing from JSON show up as undefined in their column; add defaultContent: '' to suppress the warning.

How do I add a custom “Action” column with edit/delete buttons?

Use columns.render for one column: { data: null, render: (data, type, row) => `<a href="/edit/${row.id}">Edit</a>` }. data: null tells DataTables there’s no field to read; render generates the cell HTML from the whole row object.

Related guides

  • How to Create Ajax-Based Pagination in DataTables
  • How to Change the Default Sort Order in DataTables
  • How to Add an HTML Column in Laravel DataTables

References

DataTables Ajax docs: datatables.net/manual/ajax. ajax.dataSrc: datatables.net/reference/option/ajax.dataSrc. columns.render: datatables.net/reference/option/columns.render.

TAGGED:ajaxdatatablesJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Keep only the digits in a JavaScript string with regex How to Keep Only Numbers in a String with JavaScript
Next Article Make ApexCharts.js take full width of its parent How to Make ApexCharts.js Take the Full Width of Its Parent
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Get the index in a jQuery each loop
Web Development

How to Get the Index in a jQuery .each() Loop

4 Min Read
Make ApexCharts.js take full width of its parent
Web Development

How to Make ApexCharts.js Take the Full Width of Its Parent

5 Min Read
Laravel Eloquent group by count — Book::groupBy('author') query with bar-chart aggregate icon
Web Development

How to Count Records Grouped By a Column in Laravel Eloquent

7 Min Read
Enforce a minimum phone-number length in WooCommerce checkout
Web Development

How to Set a Minimum Length for Phone Numbers in WooCommerce

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?