preXhr
Ajax event - fired before an Ajax request is made.
Description
This event is triggered prior to DataTables making an Ajax request to the server for data, and is thus the counterpart to the xhr event which is triggered after the Ajax request has successfully returned data from the server.
The preXhr event is designed for use by plug-in authors who can listen for this event in their extension and send any additional data that might be required. It is very similar to the ajax.data in this ability, with that option typically being used by those writing initialisation code for their table, rather than plug-in authors.
Please note that, as with all DataTables emitted events, the event object has a DataTables API instance available on it in the dt property.
Type
function( e, settings, data, xhr )
Parameters:
| Name | Type | Optional | |
|---|---|---|---|
| 1 | e | No | |
Event object | |||
| 2 | settings | No | |
DataTables settings object | |||
| 3 | data | No | |
Data that will be sent to the server in an object form | |||
| 4 | xhr | No | |
Since 2.0.0: The XHR object constructed for jQuery | |||
Example
Send additional data to the server on an Ajax request using an event:
new DataTable('#example', {
ajax: 'data.json',
on: {
preXhr: (e, settings, data) => {
data.sessionId = document.querySelector('#sessionId').value;
}
}
});
// Note that `ajax.data` could also be used to perform this action in this caseRelated
The following options are directly related and may also be useful in your application development.