function getUrl(url) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
console.log(req);
req.open("GET", url, true);
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200 || req.status === 304) {
resolve(req.responseText);
}
} else {
reject(new Error(req.statusText));//onerror
}
}
req.onerror = function () {
reject(new Error(req.statusText));
}
req.send();
})
}
getUrl("./index.html").then(function (text) {
console.log(text);
}).catch(function (e) {
console.log(e);
})
above is the process of using pormise to encapsulate the process of obtaining url.". / index.html" is a path that does not exist and returns 404. At first, I want to use onerror to catch errors, but find that it does not work, and then move reject to it. After that, catch can output error messages, but the console will still report an error
is there any way to prevent browsers from reporting errors
(the core of the problem is how to prevent the console from outputting 404 notfound, although a lot of data also means that browsers can"t block output, but I don"t think this problem is worthless. It"s possible that some awesome people know how to prevent browsers from outputting such information. For those who step on this problem, I can only say that the problem that has not been solved does not mean that it cannot be solved. Some people say that it cannot be solved, but it does not mean that all people cannot solve it.)