Alright, so I tried to use .htaccess to set up ErrorDocuments for various error codes. It works great, except that now, the following jQuery AJAX code will never run the error() function:
$.ajax({url: url, type: "GET", cache: false, error: function(){
alert("Looking rather erroneous, are we?");
}, success: function(html){
// ...
}
Any proposals? I think I know the reason why: .htaccess points all errors like so:
ErrorDocument 404 /error.php
And /error.php has the following:
<?php header("Location: /#error"); ?>
So when it transfers to index.php, it probably loses the 404 document status.
What would you suggest?
Try passing a header along with your AJAX request:
…which you can then detect in your
error.php, to treat errors from AJAX requests differently:You may have to experiment with that
$_SERVERkey. See alsoapache_request_headersif you’re using Apache/mod_php.EDIT:
The reason you need to do this is, as stated below, the
header("Location: /#error");sends a 302 (redirect) header, which overrides the 404 (not found). You can’t use theLocation:header to redirect on a 404 (it only works on 3xx) and you can only send one status. JQuery will (correctly) only trigger the error callback on an error status; these are all 4xx.Wikipedia‘s explanation of this is very good.