, , , , XMLHttpRequest ( fetch API). , , , , . xhr, :
function request(url, method = "GET") {
const xhr = new XMLHttpRequest();
return new Promise((res, rej) => {
xhr.open(method, url);
xhr.onload = () => {
if (xhr.status !== 200) {
return rej("Upload failed. Response code:" + xhr.status);
}
return res(xhr.responseText);
};
xhr.send();
});
}
, HTTP-...
request("http://blah.com")
.then(data => console.log(`got data: ${data}`))
.catch(e => console.error(`error: ${e}`));