+1 for this method, is pretty standard in other frameworks too. Also, according to http://www.w3.org/TR/XMLHttpRequest/#send , the Accept header must be set as '*/*'.
(and I think that there is one too much "lower()"), : ... if request.env.http_x_requested_with.lower()=='xmlhttprequest': ...