-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajaxDBQuery.js
executable file
·52 lines (44 loc) · 1.44 KB
/
ajaxDBQuery.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
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
function ajaxDBQuery(element, callback = () => { }) {
console.log("ajaxDBQuery");
if (element.dataset.url == null || element.dataset.db == null || element.dataset.table == null || element.dataset.columns == null) {
element.innerText = "400 (Bad Request)";
return;
}
const xhr = new XMLHttpRequest(),
method = "POST",
url = element.dataset.url;
var data = {
"db": element.dataset.db,
"table": element.dataset.table,
"columns": element.dataset.columns,
"inner_join": element.dataset.inner_join,
"where": element.dataset.where,
"order_by": element.dataset.order_by,
"direction": element.dataset.direction,
"limit": parseInt(element.dataset.limit, 10),
"offset": parseInt(element.dataset.offset, 10)
};
var data = JSON.stringify(data);
xhr.open(method, url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status == 200) {
// retrieve data
callback(element, this.responseText);
} else if (this.status >= 500) {
// internal server error
element.innerText = this.statusText;
} else if (this.status >= 402 && this.status <= 420) {
// error
element.innerText = this.statusText;
} else if (this.status == 400 || this.status == 401) {
// bad request & unauthorized error
element.innerText = this.statusText;
}
}
}
xhr.send(data);
}
export default ajaxDBQuery;