I use an ajax GET request for a particular file in order to check if the server is online. The console log has an error message ("net::ERR_CONNECTION_REFUSED") for every file-request made when that server is not online. The server is on a local network (at a 10.0.0.xxx address). My JavaScript code checks every second because I want to know immediately when the server is back online.
-
duplicate https://stackoverflow.com/questions/4500741/suppress-chrome-failed-to-load-resource-messages-in-console – Garr Godfrey Jul 20 '21 at 22:03
-
Thanks @Garr. This is similar to my problem. The take-away for me from this other posting thread is the idea of intercepting the console messages and ignoring the one in question. – John Mullaney Aug 01 '21 at 12:32
2 Answers
There is no way suppress these errors, however in your case you may be able get away by setting httpRequest.timeout
to something very low like 1000
(1 sec). It will timeout without errors in the console.
[UPDATE] I'm not sure if my browser was updated (Edge 92.0.902.62) but I'm no longer able reproduce that error:
!function loop()
{
const req = new XMLHttpRequest();
const callback = e =>
{
if (req.readyState != 4)
return;
const isOnline = req.status == 200;
console.log(isOnline ? "online" : "offline");
req.abort();
setTimeout(loop, isOnline ? 1000 : req.timeout);
}
req.onreadystatechange = callback
req.timeout = 500;
req.open('HEAD', 'http://localhost', true);
req.send();
}()

- 9,466
- 2
- 21
- 37
-
Thanks for this suggestion. I had to bring `httpRequest.timeout` down to 20 ms before I saw a reduction in console error messages. The console error messages are almost eliminated with a timeout of 15 ms, and are completely gone at 10 ms. However, even though my server is on the local network and is dedicated to it's purpose, a 10 ms timeout value is giving me some timeouts even when the server is online. Can you tell me about the mechanism/entity that is producing the console message, and how this is beat by the timeout happening sooner? – John Mullaney Aug 01 '21 at 12:42
-
-
My testing is done mostly with Chrome, but I tried Edge 92.0.902.62 and got the same results that I did with Chrome, i.e., got messages in the log until I took the ajax timeout under 15 ms. I saw that your call to `req.open()` uses 'HEAD' instead of the "GET' I am using. So I tried using 'HEAD' but I see the same results as I did before. – John Mullaney Aug 03 '21 at 11:56
JavaScript was not meant for this kind of business. So, only a kiond of a substitute routine can be possible to utilize as a meas that can show you if that server is on line.
Images are tailored as independent objects which can demand their actual content over the network and from the server before being added to the document.
They also feature the highly convenient onload
and onerror
events.
Which when put in a certain conditional context can mean exactly what you need to know - is server online or not. But for this to be true you need to make absolutely sure that the test image is correct and that the image file itself is physically in place.
However the the best solution to keep your console clean from network errors is to change your console settings. This is in case you are using Chrome.
function isServerOnline( url, i ) {
i = new Image( );
i.src = url + "?" + Date.now();
i.onload = () => console.info( "server is online!" );
i.onerror= () => {
console.warn( "server is offline!"),
setTimeout( isServerOnline, 2000, url)
};
}
;
isServerOnline( "test.png" );

- 5,707
- 2
- 24
- 26
-
Thanks for this suggestion. I had started out using an image-load to check if the server is online but I had to abandon that approach. The load-image approach would work if I wanted to check for the server just once, but I need to check frequently so that I know asap when the server comes online. I used setTimeout() to set a timeout for how long I would wait or an image to load, but I couldn't then abort that request to the server. So I ended up accumulating more and more outstanding requests. I forget exactly what happened, but I think I ran into some sort of resource limit. – John Mullaney Aug 03 '21 at 12:08
-
@JohnMullaney, that's odd because the `img` will not fire the *error event* anymore once the server came online. The *image* will only fire up the *error event*, if and only if the server is offline. In which case it doesn't count requests, because it's dead. and *the test image* can have 0 bytes length. – Bekim Bacaj Aug 03 '21 at 12:35