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
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Laravel unknown column CONCAT fix — DB::raw and selectRaw bypass identifier escaping
Web Development

How to Fix “Unknown column ‘CONCAT'” in Laravel

8 Min Read
WooCommerce remove checkout fields — woocommerce_checkout_fields filter unsetting fields
Web Development

How to Remove Checkout Fields in WooCommerce

6 Min Read
Laravel Blade Time Format (HH:MM)
Web Development

How to Show Only Hours and Minutes in Laravel Blade (HH:MM)

3 Min Read
JavaScript format number with decimals — toFixed, Math.floor, and Intl.NumberFormat
Web Development

How to Format a Number with Decimals in JavaScript

5 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?