preXhr

Since: DataTables 1.10

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:

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 case

Related

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