How to return server error messages to datatables.
How to return server error messages to datatables.
I'm creating a page with a table , getting data from a php script via Ajax.
My code is something like that:
$("#tblErrors").DataTable({
"ajax": {
url: "ajax/listRecords.php"
},
"columns": [
{ "data": "field_name_1" },
{ "data": "field_name_2" },
{ "data": "field_name_3" },
{ "data": "field_name_4" }
]
});
It is working fine. Now i want that when a error ocurrs in the server, the message gets passed somehow to the client. I thought about returning:
{data: [{field_name_1: "Complete error message", field_name_2: "", field_name_3: "", field_name_4: ""}]}
but I`d like to make this handling in a global function, so I won't know the name of the fields that should be returned.
So this is the question: How can i pass a error message in ajax response and make it been show to the user somehow? It doesn't need to be show in the table, it could be a alert (It would be better, actually).
Answers
I've just seen this post, that solves my problem: https://datatables.net/manual/server-side#Returned-data
Sorry for asking without searching better.
'm glad you solved the problem. I also used this link when I had a similar problem.
Just one more question. This worked for me , it showed the message I returned from the server, but it shows a message like this:
DataTables warning: table id=tblErros - My Custom Error Message
I'd like to show it more friendly to the user, showin only My Custom Error Message.