两者之间有什么不同吗
XHRGet
和
FetchGet
根据发送到的服务器的最终请求,函数如下所示
url
? 它们是否有不同的默认标题,或者类似的内容?在使用这两种方法进行网络爬行时,我注意到
fetch
往往比
XMLHttpRequest
,我不知道为什么会这样。
(async () => {
console.log( await XHRGet("https://stackoverflow.com") );
console.log( await fetchGet("https://stackoverflow.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());
}
谢谢