I'm currently checking the status code of the current page with this code :
$.ajax({
type: 'HEAD',
url: window.location.href,
success: function() {
console.log("Ok");
},
error: function() {
console.log("error");
}
});
The page exists if response's code is between 200 - 299 or not if response's codes between 400 - 499.
But for exemple a 405 error is not really an error for me, the page displays well, so no reason to be in the error state.
So I added an another test :
error: function(jqXHR, textStatus, errorThrown) {
console.log('jqHRX : ' + JSON.stringify(jqXHR) + ' textStatus : ' + textStatus + ' errorThrown : ' + errorThrown);
if (errorThrown != "OK") //or jqHRX.statusText
console.log("error");
}
For exemple, if I browse to a page which returns a 405 code, I'll have this output :
jqHRX : {"readyState":4,"responseText":"","status":405,"statusText":"OK"} textStatus : error errorThrown : OK
And if it's a 404 error :
jqHRX : {"readyState":4,"responseText":"","status":404,"statusText":"Not Found"} textStatus : error errorThrown : Not Found
error
- So, are all pages with a errorThrown equals to "OK" currently available ? I found the exemple of 405 by chance so.. I don't really know if it's a isolated case or not.
- Is there a better way to handle this ?
Thanks !
Alex
edit : for exemple, https://developer.chrome.com/home returns a 405 with my code but in my application there is no need to consider this like an error.
Strange thing, you can verify the statusCode with http://tools.seobook.com/server-header-checker/ : the result is 405 But if you check with http://httpstatus.io/ the result is 200