forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAJAX_request.js
More file actions
29 lines (24 loc) · 703 Bytes
/
Copy pathAJAX_request.js
File metadata and controls
29 lines (24 loc) · 703 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
25
26
27
28
29
const POST = "POST";
export class AJAXRequest {
constructor () {
// eslint-disable-next-line no-undef
this.request = new XMLHttpRequest();
}
open (method, url, async) {
this.method = method;
this.request.open(method, url, async);
}
send (string) {
const args = this.method === POST ? [string] : [];
this.request.send.apply(this.request, args);
}
}
// OR closure approach
// eslint-disable-next-line no-undef
const request = new XMLHttpRequest();
let usedMethod;
export const open = (method, url, async) => {
usedMethod = method;
request.open(method, url, async);
};
export const send = string => (usedMethod === POST ? request.send(string) : request.send());