summaryrefslogtreecommitdiff
path: root/day9/task5/static/js/utils.js
blob: d76c5472b8173a960734f64b3c9f1d5a3cd68627 (plain)
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
const request = {
            get: function (url, callback) {
                let xmlHttp = new XMLHttpRequest();
                xmlHttp.onreadystatechange = function() {
                    if (xmlHttp.readyState === 4 && xmlHttp.status === 200)
                        callback(xmlHttp.responseText);
                };
                xmlHttp.open("GET", url, true);
                xmlHttp.send();
            },

            post: function (url, callback, data) {
                let xmlHttp = new XMLHttpRequest();
                xmlHttp.onreadystatechange = function() {
                    if (xmlHttp.readyState === 4 && xmlHttp.status === 200)
                        callback(xmlHttp.responseText);
                };
                xmlHttp.open("POST", url, true);
                xmlHttp.send(this.formatParams(data));
            },

            formatParams: function (params) {
                return Object.keys(params).map((key) => {
                    return key+"="+encodeURIComponent(params[key])
                }).join("&")
            }
        };

function elementFromHTML(html) {
    let elem = document.createElement('div');
    elem.innerHTML = html;
    return elem.firstChild;
}