确定由于CORS问题导致的抓取错误

我想告诉您资源加载是否由于CORS错误而失败,因为我想使用我在服务器上设置的代理。除非绝对必要,否则我不希望使用我的代理服务器,因为会通过它传递很多额外的数据。

当我这样做时:

fetch("https://some-resource-somewhere-that-a-user-has-provided")
    .then(response => {
      // do stuff with the response
    }).catch((e) => {
       // e === Error("TypeError: Failed to fetch")
    });

该错误(至少在chrome上)无济于事,但是控制台中有红色的大文字,向我提供了我所需要的信息!

我怎么知道

  

TypeError:无法获取

是CORS问题,还是其他一些负载问题?

xuming_11 回答:确定由于CORS问题导致的抓取错误

我想出了以下解决方案:

var xhttp = new XMLHttpRequest();
xhttp.onerror = function () {
  if (this.status === undefined || this.status === 0) {
    console.log('error due to CORS issue');
  } else {
    console.log("** An error occurred during the transaction",this);
  }

};
xhttp.open("GET","https://stackoverflow.com",true);
xhttp.send();
本文链接:https://www.f2er.com/3168068.html

大家都在问