When calling AJAX from Javascript, callbacks of ‘success’ and ‘error’ are typically supported. Your call might look something like this (I happen to be using Angular here):
$http( request ).success( function( response ) { console.log( 'SUCCESS: ' + response ); }).error( function( response ) { console.log( 'ERROR: ' + response ); });
Your server code, possibly written in PHP, might look something like this:
function some_code_here() { echo 'everything is cool'; exit; }
No matter what, the ‘success’ callback is always triggered. So, how would you trigger the ‘error’ callback? Hmm. This had me reeling for a bit. Then found that this works:
function some_code_here() { $whatever = true; if ( $whatever ) { header( 'HTTP/1.1 500 Internal Server Error' ); echo 'some error happened'; exit; } }
You can return any error value that you see on this page:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error
The process of forcing an error is completely not intuitive, which is why I’ve posted it here for future reference. Hope it helps you too.