0

所以三天多来,我一直在寻找如何从控制台隐藏这个错误并响应这个错误:

加载资源失败:net::ERR_NAME_NOT_RESOLVED

Google Chrome 控制台中的错误图像,我想隐藏和处理

官方 chrome 文档都没有帮助。https://developers.google.com/web/tools/chrome-devtools/console/track-exceptions

也没有我在谷歌搜索中出现的任何技巧。

产生这个不可抗拒的错误发生的 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>

4

3 回答 3

1

现在是 2018 年 10 月,对于这个问题仍然没有很好的解决方案。

这是 Chromium 论坛中的一个活跃线程,最后的帖子是从这个月开始的。

https://bugs.chromium.org/p/chromium/issues/detail?id=124534

显然,许多用户确实声称 4xx 响应代码是预期的和有效的,并且应该至少通过某种方式“可捕获”。

于 2018-10-22T18:04:14.173 回答
-1

你不能。网络相关错误的原因不会暴露给 JS。它们就像同源策略违规一样被隐藏。

(如果可能的话,网站可以遍历私有 LAN 上使用的名称列表(例如 Intranet 服务器和 IoT 设备使用的名称),检测错误并识别 LAN 上的设备。然后可以将这些信息用于发动进一步的攻击。)

于 2017-12-13T14:59:56.817 回答
-1

尝试这样做

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);

如果我的回答不够清楚,您也可以查看此链接。这个也是

更新

您将无法从控制台中删除一个特定错误。清除它的唯一方法是尝试这样的事情:

httpRequest.onerror = function (e) {
    document.getElementById("information").innerHTML = "Error Unresponsive Domain";
    console.clear(); //clear console
};
httpRequest.send(null);

我也偶然发现了这个,但我不知道它是否会帮助你。

于 2017-12-13T16:21:36.707 回答