如果URL不存在,则防止出现错误消息

时间:2020-01-14 13:54:11

标签: javascript

我尝试通过两种方法找出URL是否存在。

选项1

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

选项2

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

他们两个都能工作,但我仍然收到错误消息:

HEAD http://localhost:4200/api/uploads/profile-images/a68e2578adb131bd8c6abfde2c729055.png 404(未找到)

在确定URL不存在之后,我不想显示此错误。如何防止该错误?

0 个答案:

没有答案
相关问题