I'm loading some data via $.ajax() from an api on a different server. CORS is setup so that I can load data from this API.
But, sometimes, the API, written in php, displays some error/warning, which it will output before the http headers is sent. Which again will make the $.ajax()-request fail with the message: "XMLHttpRequest cannot load
http://<server>pdf/A5Landscape. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '
http://intra.quercus.palustris.dk' is therefore not allowed access."
When this happens, my code sort of freezes. The $.ajax()-call looks like this:
$.ajax({...})
.done(function() {
console.log('done')
})
.fail(function() {
console.log('fail');
})
.always(function() {
console.log('finished');
});
But when the above CORS error appears, neither the fail() or the always()-part is called.
How can I catch this error, to have my code display a friendly message explaining the problem?
-mads