summaryrefslogtreecommitdiff
path: root/htdocs/js/common/02-ajax.js
blob: c432a51d977d802204265dfb4606b591b0c7b0cc (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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// AJAX
//
(function() {

    var defaultOpts = {
        json: true
    };

    function createXMLHttpRequest() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }

        var xhr;
        try {
            xhr = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try {
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {}
        }
        if (!xhr) {
            console.error('Your browser doesn\'t support XMLHttpRequest.');
        }
        return xhr;
    }

    function request(method, url, data, optarg1, optarg2) {
        data = data || null;

        var opts, callback;
        if (optarg2 !== undefined) {
            opts = optarg1;
            callback = optarg2;
        } else {
            callback = optarg1;
        }

        opts = opts || {};

        if (typeof callback != 'function') {
            throw new Error('callback must be a function');
        }

        if (!url) {
            throw new Error('no url specified');
        }

        switch (method) {
            case 'GET':
                if (isObject(data)) {
                    for (var k in data) {
                        if (data.hasOwnProperty(k)) {
                            url += (url.indexOf('?') == -1 ? '?' : '&')+encodeURIComponent(k)+'='+encodeURIComponent(data[k])
                        }
                    }
                }
                break;

            case 'POST':
                if (isObject(data)) {
                    var sdata = [];
                    for (var k in data) {
                        if (data.hasOwnProperty(k)) {
                            sdata.push(encodeURIComponent(k)+'='+encodeURIComponent(data[k]));
                        }
                    }
                    data = sdata.join('&');
                }
                break;
        }

        opts = Object.assign({}, defaultOpts, opts);

        var xhr = createXMLHttpRequest();
        xhr.open(method, url);

        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        if (method == 'POST') {
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        }

        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if ('status' in xhr && !/^2|1223/.test(xhr.status)) {
                    throw new Error('http code '+xhr.status)
                }
                if (opts.json) {
                    var resp = JSON.parse(xhr.responseText)
                    if (!isObject(resp)) {
                        throw new Error('ajax: object expected')
                    }
                    if (resp.error) {
                        throw new Error(resp.error)
                    }
                    callback(null, resp.response);
                } else {
                    callback(null, xhr.responseText);
                }
            }
        };

        xhr.onerror = function(e) {
            callback(e);
        };

        xhr.send(method == 'GET' ? null : data);

        return xhr;
    }

    window.ajax = {
        get: request.bind(request, 'GET'),
        post: request.bind(request, 'POST')
    }

})();