DataTable.ajax()
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 todata- any data to send to the servermethod- The HTTP request typesuccess- 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:
| Name | Type | Optional | |
|---|---|---|---|
| 1 | options | No | |
This object is used to configure how the Ajax request is made. Use the options to send data, change the HTTP request type, add response callbacks and more. Please see the | |||
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.