0

There are two ways that I try to figure out if a URL exists.

Option 1

function fileExists(url, callback) {
  fetch(url, { method: 'head' })
    .then((status) => {
      callback(status.ok);
    });
}

Option 2

function fileExists(url) {
  const http = new XMLHttpRequest();
  http.open('HEAD', url, false);
  http.send();
  return http.status !== 404;
}

Both of them work but I still get the error:

HEAD http://localhost:4200/api/uploads/profile-images/a68e2578adb131bd8c6abfde2c729055.png 404 (Not Found)

I don't want to show this error after I have figured out that the URL does not exist. How can I prevent that error?

xRay
  • 543
  • 1
  • 5
  • 29
  • Does this answer your question? [How can I stop jQuery.ajax() from logging failures to the console?](https://stackoverflow.com/questions/11949095/how-can-i-stop-jquery-ajax-from-logging-failures-to-the-console) – Ivar Jan 14 '20 at 13:56
  • You probably want to `catch()` – Terry Jan 14 '20 at 14:06
  • @Ivar no. Unfortunately, it wouldn't solve my problem. – xRay Jan 14 '20 at 14:21
  • @Terry I have tried that out but the error still appears. – xRay Jan 14 '20 at 14:22
  • @Reza I understand that it doesn't really solve your problem, but the answer remains the same. That error is logged by the browser, not by the JavaScript. There is simply no way to suppress it. The only way is to prevent calling endpoints that return a 404. – Ivar Jan 14 '20 at 14:24

0 Answers0