【发布时间】:2020-08-26 10:08:16
【问题描述】:
我正在尝试创建一个 JavaScript 代码,允许我检查是否可以通过 URL 向给定主机发送 HTTP 请求(检查服务器是否打开)。这是我的代码:
<html>
<body>
<script>
function checkServer(url) {
const controller = new AbortController();
const signal = controller.signal;
const options = { mode: 'no-cors', signal };
return fetch(url, options)
.then(setTimeout(() => { controller.abort() }, 5))
.then(response => function()
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://log.mywebsite.com", true);
xhttp.send();
}
)
.catch(error => function()
{
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://log.mywebsite.com", true);
xhttp.send();
}
)
}
checkServer('https://blahdummydamnfck.com');
</script>
</body>
</html>
但是,虽然它不能向服务器发送 HTTP 请求,但它并没有向我的日志服务器发送日志请求。为什么会发生在我身上?
【问题讨论】:
标签: javascript html logging xmlhttprequest