-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathajax.js
More file actions
99 lines (81 loc) · 2.7 KB
/
ajax.js
File metadata and controls
99 lines (81 loc) · 2.7 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Simple Ajax module
// Create an Ajax object
// Settings can be the API url
// Settings can be an object the following keys: url, method and/or data
// Each key is optionnal
export var ajax = (settings=undefined) => {
// Contains the request parameters
let params = {};
// Process constructor arguments
switch (typeof settings) {
case 'string' : params.url = settings; break
case 'object' : params = settings; break;
}
// Create the data key for future use
if (!('data' in params)) params.data = {};
if (!('method' in params)) params.method = 'POST';
// Promise resolve and reject
let promise = {
resolve:null,
reject:null
}
// HTTP request object
let request = new XMLHttpRequest();
// Set URL
var url = function(url) {
params.url=url;
return this;
}
// Set method (POST or GET)
var method = function(method) {
params.method = method;
return this;
}
// Push data (key and value) or object
var data = function(objectOrKey, value) {
push(objectOrKey, value)
return this;
}
var push = function(objectOrKey, value)
{
switch (typeof objectOrKey)
{
case 'object': params.data = Object.assign(objectOrKey, params.data); break;
case 'string': params.data = Object.assign({ [objectOrKey] : value}, params.data); break;
}
}
var post = function(data)
{
params.method='POST';
return send(data);
}
var get = function(data)
{
params.method='GET';
return send(data);
}
// Send request and return a promise
var send = function(data)
{
push(data);
return new Promise((resolve, reject) => {
promise.resolve = resolve;
promise.reject = reject;
request.onreadystatechange = callBack;
request.open(params.method, params.url);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.send( Object.keys(params.data).map((key) => encodeURIComponent(key) + '=' + encodeURIComponent(params.data[key])) .join('&') );
})
}
// Callback function
var callBack = function()
{
// Request not finished
if (request.readyState != 4) return;
// Check status and reject in case of failure
if (request.status<200 || request.status>299) { promise.reject(request); return; }
// Success, resolve response
promise.resolve (request.responseText);
}
return { data:data, url:url, method:method, send:send, post:post, get:get };
}