我写了一个函数,不断返回Access-Control-Allow-Origin错误.这对我来说真的很好; 我不想解决这个问题.我只是想抓住它,所以我可以在我的程序中阅读它的消息.
导致错误抛出的所有代码都在我的try块中,我的catch块显示错误的字符串消息.但是,当我运行代码时,没有捕获错误,并且错误在控制台中显示为红色.如何捕获此错误并存储其消息?
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);
}
Run Code Online (Sandbox Code Playgroud)