Handle AJAX request when session has timed out

Problem: You are logged in to admin (and the user session has timed out). Then you use a function that performs a ajax request. What happens is this: the login page is returned as a result to the ajax request, which cannot be handled and causes confusion to the user.

This is where the magic happens: HTTP_X_REQUESTED_WITH == 'xmlhttprequest'
This works to detect ajax requests from all javascript libraries. 

Maybe there's a better solution to this, like returning a:
$json['error'] = $this->language->get('error_token');

But that requires that the ajax call functions handles it.