ServerSide Custom Error Messages

ServerSide Custom Error Messages

leemobileleemobile Posts: 3Questions: 0Answers: 0
edited March 2011 in General
Hi , I'm new to datatables and implemented server-side processing for my site with Datatables.

It works great, does sorting/filtering/pagination on the server side really well, thanks to the great example documentation.

However, I would just like to over-ride the error message that shows up for unsuccessful Ajax calls, such as when the server is down. I've been playing around with fnServerData and have been trying to find an example of how to do this. I was able to override the error message function, but I lose all the pagination/filter/sort information that is sent:

[code]
var tableParameters = {
...,
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback,
"error": function () {
alert( "My custom error message." );
}
} );
}
}
$('#my_table').dataTable(tableParameters);
[/code]

How can I override the error function for the ajax call but still retain all the pagination/sort/filter variables that are sent in the GET request?

Thanks!

Lee

Replies

  • leemobileleemobile Posts: 3Questions: 0Answers: 0
    I figured it out!

    I had to override the fnServerData function in the settings object, and not pass it in as an initialization parameter:

    [code]
    oSettings = $('#my_table').dataTable(tableParameters).fnSettings();

    oSettings.fnServerData = function ( url, data, callback ) {
    $.ajax({
    "url": url,
    "data": data,
    "success": callback,
    "dataType": "json",
    "cache": false,
    "error": function () {
    alert("Booyah!");
    }
    });
    };
    [/code]

    Thanks anyways, hopefully this'll help someone else out.
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Interesting - there is something funny going on there, as you certainly should be able to pass fnServerData as part of the initialisation, as seen in this example: http://datatables.net/examples/server_side/post.html .

    Any chance you can post an example of the page which isn't working? Although, you've got it working now - nice one :-)

    Allan
  • leemobileleemobile Posts: 3Questions: 0Answers: 0
    Ugh, it was total human error on my part.
    My server is expecting a GET and not a POST, and I had it configured on Datatables to POST.

    I configured the fnServerData like in the example, and it worked when I use the correct type.

    Probably can delete this thread since it's pretty useless now :-)
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    No problem :-). Thread might be useful for someone else - we'll leave it knocking around :-)

    Allan
This discussion has been closed.