I wrote a function that keeps returning an Access-Control-Allow-Origin error. This is actually great for me; I do not want to fix this. I just want to catch him so that I can read his message in my program.
All the error code gets into my try block, and my catch block displays an error message. However, when I run the code, the error is not detected, and the error is displayed in red in the console. How can I catch this error and save her message?
try {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (this.status < 400 && this.status >= 300) {
console.log('this redirects to ' + this.getResponseHeader("Location"));
} else {
console.log('doesn\'t redirect');
}
}
xhr.open('HEAD', $scope.suggLink, true);
xhr.send();
} catch(e) {
console.log('Caught it!');
console.log(e.message);
}
source
share