`XMLHttpRequest` 和 `fetch` 的默认 GET 行为之间的区别?

Differences between default GET behaviour of `XMLHttpRequest` and `fetch`?

就发送到 url 服务器的最终请求而言,下面的 XHRGetFetchGet 函数之间是否存在任何差异?他们有不同的默认 headers 或类似的东西吗?在使用这两种方法进行网络抓取时,我注意到 fetchXMLHttpRequest 更容易失败,我不确定为什么会这样。

(async () => {
  console.log( await XHRGet("https://whosebug.com") );
  console.log( await fetchGet("https://whosebug.com") );
})();

function XHRGet(url) {
  return new Promise(resolve => {
    let req = new XMLHttpRequest();
    req.addEventListener("load", function() { resolve(this.responseText); });
    req.open("GET", url);
    req.send();
  });
}

function fetchGet(url) {
  return fetch(url).then(res => res.text());
}

谢谢!

fetch() 默认不包含凭据。我希望在 https://github.com/whatwg/fetch/pull/585 中改变它。之后这些应该几乎相同。 (还有一些解码差异。fetch() 将始终使用 UTF-8。XMLHttpRequest 更宽松一些。但这不会导致失败,只是可能会导致不同的结果字符串。)