Strange troubles with sEcho

Strange troubles with sEcho

TomasVTomasV Posts: 2Questions: 0Answers: 0
edited March 2010 in General
Hi,

first of all - thanks - I've hacked something like this recently, but this looks awesome!

Anyway, I'm quite new to jquery and I've been playing with DataTables for a single day, but I had some strange problems with the 'server-side data' example - no matter what I did the table remained empty and a message 'processing' was displayed. I've actually downloaded the page and used exactly the same data, but still it did not work - I've seen the data are downloaded (thanks to the access log) but it was not displayed.

In the end I've debugged the script and I've noticed that it 'fails' thanks to line 2758 (in the _fnAjaxUpdateDraw function):

if ( json.sEcho*1 < oSettings.iServerDraw )
{
return;
}

because the data contain "sEcho = 0" and the iServerDraw = 1, the function silently returns. Once I removed the sEcho from the data, it works. So I have two questions:

1) What is sEcho supposed to do? I guess it's a way to prevent stale / cached data, right?

2) Why does it work for you (on your site), but once I download it it does not work anymore?

Thanks

Replies

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    1. sEcho is used for sequencing. If you perform multiple operations very quickly with server-side processing, multiple-sever requests can be sent out, and may not return in the same order that they were sent out. sEcho ensures that DataTables will draw the latest return only and discard old ones.

    2. The most common cause is that JSON return from the server is not valid. If you put the return through http://jsonlint.com - is it valid? The next most common reason is that sEcho is not being returned exactly as it was sent (although cast as an 'int' on the server-side). Either of these apply?

    Allan
  • TomasVTomasV Posts: 2Questions: 0Answers: 0
    OK, thanks for explaining the purpose of 'sEcho' - I'm used to the traditional 'sequential' way and I tend to forget about the 'asynchronous' part of the AJAX.

    And I think this explains why the 'copy' of the example does not work - I have used a static copy of the data, i.e. I've downloaded the data from http://www.datatables.net/examples/examples_support/server_processing.php and stored them in a text file. But this way there is no 'sEcho' value in the request so the 'static' data contain just 'sEcho':0 and do not reflect the value from the AJAX requests (made when loading the data by the table). Now it's clear.

    Anyway the only explanation of 'sEcho' (purpose and how it works) I've found is located at http://www.datatables.net/usage/server-side and I think it's unsufficient. For new developers (e.g. me) this may be very confusing so I'd recommend to put there a clear description that the request has to contain the 'sEcho' value sent in the request (or no 'sEcho' at all), and I'd put the same notice to the "DataTables server-side processing example" page too.
  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin
    Hi TomasV,

    Good to hear that did the trick for you :-). What I've done to hopefully address this a little bit in future is to tidy up the server-side usage page a bit to note the use sEcho a little bit more: http://www.datatables.net/usage/server-side . I've also added this as an FAQ: "Its still not working..." http://datatables.net/faqs . Hopefully this will help others in future, along with this thread.

    Regards,
    Allan
  • gutzoftergutzofter Posts: 58Questions: 0Answers: 0
    I would recommend that you deprecate the sEcho variable to something like sRequestSequence. I'm unit testing an object-oriented version of server side in PHP. Building the JSON is always nice to know what the variables are. I know since you've already publishes it as part of the public interface it might be tough. Maybe using either in your JSON would work for a while.
This discussion has been closed.