-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.js
39 lines (32 loc) · 1.09 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let xhr = new XMLHttpRequest();
xhr.open('GET', 'https://cors-anywhere.herokuapp.com/https://postman-echo.com/get?foo1=bar1&foo2=bar2');
// xhr.open('POST', 'https://cors-anywhere.herokuapp.com/https://postman-echo.com/post?foo1=bar1&foo2=bar2');
xhr.setRequestHeader('Authorization', 'Bearer token');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.send();
// xhr.send(JSON.stringify(
// {
// a: 1,
// b: 2
// }
// ));
xhr.onload = function() {
if (xhr.status != 200) { // analyze HTTP status of the response
alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
console.log(xhr);
} else { // show the result
alert(`Done, got ${xhr.response.length} bytes`);
console.log(xhr.response); // response is the server
}
};
xhr.onprogress = function(event) {
if (event.lengthComputable) {
alert(`Received ${event.loaded} of ${event.total} bytes`);
} else {
alert(`Received ${event.loaded} bytes`); // no Content-Length
}
};
xhr.onerror = function(error) {
console.log('Request failed', error);
};