Using Ajax and jQuery to check if file exists - always returns 200 response
Why don't you realize this asynchronously with the callbacks success and error?
$.ajax({
type: 'HEAD',
url: fileLocation,
success: function(msg){
alert(msg);
},
error: function(jqXHR, textStatus, errorThrown){
log(jqXHR);
log(errorThrown);
}
});
It appeared my issued existed with my custom 404 page, which was returning a 200 status code. I had to hard code the 404 response code using the php header() function, which resolved the issue I was having. Now if the page does not exists it follows correctly:
Using a simple method to test if page/file exists for the moment:
$.ajax({
type: 'HEAD',
url: 'http://www.example.com/index.php',
success: function() {
alert('Page found.');
},
error: function() {
alert('Page not found.');
}
});
Thanks to @kalyfe for the suggestion to switch to async method.