DataTable.ajax()

Since: DataTables 3

Make an Ajax call to a server.

Description

DataTables makes it easy to fetch remote data through its ajax option. The Ajax call that it makes is done through this API method which is exposed so you can make use of the simple interface that it offers if you wish to make your own Ajax calls.

This static method is largely based on jQuery's ajax() method, but it does not implement as many options, only those most commonly used by DataTables. It also provides options that are useful for DataTables that jQuery does not provide.

Configuration of the Ajax request done through the object given to this object, which is of type DataTable.AjaxOptions. The most commonly used options will be:

  • url - address to make the Ajax request to
  • data - any data to send to the server
  • method - The HTTP request type
  • success - A callback function to execute when the request is complete.

For a complete list of the options that can be used and full details for each one, please refer to the DataTable.AjaxOptions documentation.

Please be aware that this method is an alias to DataTable.util.ajax, which can be imported using ES modules (see example below).

Type

ajax( options )

Make an Ajax request to send and get data to a server.

Parameters:

Examples

Make a simple GET request with no extra data:

DataTable.ajax({
	success: (json) => {
		// Do something with `json`
	},
	url: '/api/data'
});

Make a POST request with data:

DataTable.ajax({
	data: {
		userId: 1
	},
	method: 'POST',
	success: (json) => {
		// Do something with `json`
	},
	url: '/api/data'
});

Make a POST request with data:

DataTable.ajax({
	data: {
		userId: 1
	},
	method: 'POST',
	success: (json) => {
		// Do something with `json`
	},
	url: '/api/data'
});

ES module use:

import { util } from 'datatables.net';

util.ajax({
	success: (json) => {
		// Do something with `json`
	},
	url: '/api/data'
});

Related

The following options are directly related and may also be useful in your application development.