At the top of your ajax script, add
// Temporary hack until MDL-38846 is fixed. Once that is fixed, these two lines,
// (of code, plus this comment.) and the line at the bottom can be deleted.
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
@header($protocol . ' 500 Internal Server Error');
and at the end (after all the processing is done, and before you send the response add).
// Other line to delete once MDL-38846 is fixed
@header($protocol . ' 200 OK');
That way you will get a 500 status code if an error occurs, and a 200 if no error occurs.
We need to tell the JavaScript that it failed! Otherwise errors may be missed.