I need to check if the request is ajax. $request->isXmlHttpRequest() works fine, however if there is a redirect somewhere during the execution, this method will
$request->isXmlHttpRequest()
Some js libraries doesn't send the XmlHttpRequest value in the headers, juste add this in your script:
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");