7. Warning: Ajax error
When using the ajax option to load data for DataTables, a general error can be triggered if the server responds with anything other than a valid HTTP 2xx response. The error given is:
DataTables warning: table id=
{id}- Ajax error
where {id} is the DOM id of the table that triggered the error.
Meaning
This occurs when the Ajax call falls into its error callback handler (this callback built into DataTables), which will typically occur when the server responds with anything other than a 2xx HTTP status code. For example the server might respond with 404 Not Found indicating that the file requested is not available at the given URL, or 500 Internal Error which indicates that the server encountered an error while processing the request.
DataTables will fire a specific error for the case where the request from the server is a valid return (200 Ok for example), but not valid JSON - see technical note 1 for more information. This error (7) indicates a general error, as stated above.
Diagnosis
If the server didn't reply to the Ajax request with a 2xx status code, we need to know what it did reply with, so we can take corrective action. So discovering what that reply was will be the starting point for resolving the issue full.
Modern browsers all come with built in developer tools, which can be used to find out what data the server is responding to the DataTables Ajax request with. To access the developer tools for your browser, right click on the page and select "Inspect" (or "Inspect Element"). A panel will pop open with various debugging options. In this case we want the "Network" panel to show details of the requests and responses that the browser makes and the server replies with.
For more details about access the developer tools in your browser, please see the approiate documentation for your browser:
Resolution
Once you have followed the instructions above you will know what error the server is responding with for the Ajax request.
The most common errors are:
- 404 - Not Found.
- The URL requested was not found on the server.
- Check for typos in the file name in the
ajaxparameter and in your file on the server.
- 500 - Internal Error.
- The server encountered an error while responding to the request.
- Check the server's error log for information as to why this error occurred.
- Origin not allowed by Access-Control-Allow-Origin.
- A request has been made to page on a different domain from the domain which the current page has been loaded from.
- Browsers have security measures built in to prevent loading of external scripts - for example on
http://localhostyou cannot Ajax load a script fromhttp://google.comwithout special measures. - If you are using a single domain, ensure you are specifying a relative or absolute path without a domain name (i.e. don't start the URL with
http(s)://). - Use JSONP or some other cross domain access technology such as a proxy script on your server.
If you are willing to accept the error (for example if you cannot alter the backend system to fix the error), but don't want your end users to see the alert() message, you can change DataTables' error reporting mechanism to throw a Javascript error to the browser's console, rather than alerting it. This can be done using:
DataTable.ext.errMode = 'throw';