1. Warning: Invalid JSON response

A warning from DataTables stating "Invalid JSON response" is one of the most common errors that can occur with DataTables. Fortunately it is fairly easy to diagnose what is going wrong, with the developer tools built into modern web-browsers. This guide shows exactly what the error means, how to diagnose the issue and how to resolve it.

Meaning

When loading data by Ajax (ajax), DataTables by default, expects the data loaded to be valid JSON. JSON is a data-interchange format that has provide to be extremely populate on the web, due to its compatibility with Javascript, its ease of use and the fact that it is easily human readable. However, like all data-interchange formats, it has strict rules about what is valid JSON. Anything that does not meet this criterion is invalid JSON, and will throw an error in DataTables where it expects JSON.

When DataTables shows the message:

DataTables warning: table id={id} - Invalid JSON response.

it means exactly what it says. DataTables made an Ajax response to the server for JSON data, but the data that it received back was not valid JSON.

Diagnosis

If the server didn't reply to the Ajax request with valid JSON data, we need to know what it did reply with, so we can take corrective action. Typically, the response will consist of an error message from the program on the server which is being used to create the JSON, which will be the starting point for resolving the issue fully.

Server data

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:

JSON validation

Once you have the Ajax data response from the server, you can validate the JSON using a tool such as JSON Lint or JSON Parser. Often the error in the JSON response from the server will be very obvious, but a JSON Linter can provide to be very useful for resolve the less obvious issues, such as trailing commas.

Resolution

Once you have followed the instructions above to find out what the data from the server actually is, since it is not JSON, you will be able to modify the data, or the program that creates the data, to return valid JSON only.