处理 JavaScript 中的 Chrome 控制台错误 (XMLHttpRequest)
Handling Chrome console errors in JavaScript (XMLHttpRequest)
所以三天多来,我一直在寻找如何从控制台隐藏此错误并响应此错误:
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
Error image in the Google Chrome's console, that I want to hide and handle
官方 chrome 文档均无帮助。 https://developers.google.com/web/tools/chrome-devtools/console/track-exceptions
也没有我在 google 搜索中遇到的任何技巧。
产生这个不可避免的错误发生的JavaScript
(用于快速测试处理解决方案)
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
information: <div id="information" ></div>
<script>
var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", "https://sdajfkljsdfk.lt", true);
httpRequest.onload = function (e) {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
console.log(httpRequest.responseText);
} else {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
}
}
};
httpRequest.send(null);
</script>
</body>
</html>
你不能。网络相关错误的原因没有暴露给JS。它们就像违反同源策略一样被隐藏。
(如果可能的话,网站可以遍历专用 LAN 上使用的名称列表(例如 Intranet 服务器和 IoT 设备使用的名称),检测错误并识别 LAN 上有哪些设备。此信息随后可用于发起进一步的攻击。)
尝试这样做
httpRequest.open("GET", "https://sdajfkljsdfk.lt", true);
httpRequest.onload = function (e) {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
console.log(httpRequest.responseText);
} else {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
}
}
};
httpRequest.onerror = function (e) {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
};
httpRequest.send(null);
你也可以看看this link if my answer is not clear enough. This one too
更新
您将无法从控制台删除一个特定错误。清除它的唯一方法是尝试这样的事情:
httpRequest.onerror = function (e) {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
console.clear(); //clear console
};
httpRequest.send(null);
我也是偶然发现的,不知道对你有没有帮助。
现在是 2018 年 10 月,这个问题仍然没有很好的解决方案。
这是 Chromium 论坛中的一个活跃话题,最后的帖子来自本月。
https://bugs.chromium.org/p/chromium/issues/detail?id=124534
显然,许多用户确实声称 4xx 响应代码是预期的且有效的,并且应该至少 "catch-able" 通过某种方式。
所以三天多来,我一直在寻找如何从控制台隐藏此错误并响应此错误:
Failed to load resource: net::ERR_NAME_NOT_RESOLVED
Error image in the Google Chrome's console, that I want to hide and handle
官方 chrome 文档均无帮助。 https://developers.google.com/web/tools/chrome-devtools/console/track-exceptions
也没有我在 google 搜索中遇到的任何技巧。
产生这个不可避免的错误发生的JavaScript
(用于快速测试处理解决方案)
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
information: <div id="information" ></div>
<script>
var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", "https://sdajfkljsdfk.lt", true);
httpRequest.onload = function (e) {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
console.log(httpRequest.responseText);
} else {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
}
}
};
httpRequest.send(null);
</script>
</body>
</html>
你不能。网络相关错误的原因没有暴露给JS。它们就像违反同源策略一样被隐藏。
(如果可能的话,网站可以遍历专用 LAN 上使用的名称列表(例如 Intranet 服务器和 IoT 设备使用的名称),检测错误并识别 LAN 上有哪些设备。此信息随后可用于发起进一步的攻击。)
尝试这样做
httpRequest.open("GET", "https://sdajfkljsdfk.lt", true);
httpRequest.onload = function (e) {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
console.log(httpRequest.responseText);
} else {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
}
}
};
httpRequest.onerror = function (e) {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
};
httpRequest.send(null);
你也可以看看this link if my answer is not clear enough. This one too
更新
您将无法从控制台删除一个特定错误。清除它的唯一方法是尝试这样的事情:
httpRequest.onerror = function (e) {
document.getElementById("information").innerHTML = "Error Unresponsive Domain";
console.clear(); //clear console
};
httpRequest.send(null);
我也是偶然发现的
现在是 2018 年 10 月,这个问题仍然没有很好的解决方案。
这是 Chromium 论坛中的一个活跃话题,最后的帖子来自本月。
https://bugs.chromium.org/p/chromium/issues/detail?id=124534
显然,许多用户确实声称 4xx 响应代码是预期的且有效的,并且应该至少 "catch-able" 通过某种方式。