-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAjax.js
More file actions
24 lines (23 loc) · 672 Bytes
/
Ajax.js
File metadata and controls
24 lines (23 loc) · 672 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export function requestText(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response);
} else {
reject(xhr.statusText);
}
};
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
}
export function requestImage(url) {
return new Promise((resolve, reject) => {
let img = new Image();
img.src = url;
img.onload = () => resolve(img);
img.onerror = (e) => reject(e);
});
}