'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
var isFn = require('./isFn');
var noop = require('./noop');
var defaults = require('./defaults');
var isObj = require('./isObj');
var query = require('./query');
exports = function(options) {
    defaults(options, exports.setting);
    var type = options.type;
    var url = options.url;
    var data = options.data;
    var dataType = options.dataType;
    var success = options.success;
    var error = options.error;
    var timeout = options.timeout;
    var complete = options.complete;
    var xhr = options.xhr();
    var abortTimeout;
    xhr.onreadystatechange = function() {
        if (xhr.readyState !== 4) return;
        clearTimeout(abortTimeout);
        var result;
        var status = xhr.status;
        if ((status >= 200 && status < 300) || status === 304) {
            result = xhr.responseText;
            if (dataType === 'xml') result = xhr.responseXML;
            try {
                if (dataType === 'json') result = JSON.parse(result);
            } catch (e) {}
            success(result, xhr);
        } else {
            error(xhr);
        }
        complete(xhr);
    };
    if (type === 'GET') {
        data = query.stringify(data);
        if (data) url += url.indexOf('?') > -1 ? '&' + data : '?' + data;
    } else if (options.contentType === 'application/x-www-form-urlencoded') {
        if (isObj(data)) data = query.stringify(data);
    } else if (options.contentType === 'application/json') {
        if (isObj(data)) data = JSON.stringify(data);
    }
    xhr.open(type, url, true);
    xhr.setRequestHeader('Content-Type', options.contentType);
    if (timeout > 0) {
        abortTimeout = setTimeout(function() {
            xhr.onreadystatechange = noop;
            xhr.abort();
            error(xhr, 'timeout');
            complete(xhr);
        }, timeout);
    }
    xhr.send(type === 'GET' ? null : data);
    return xhr;
};
exports.setting = {
    type: 'GET',
    success: noop,
    error: noop,
    complete: noop,
    dataType: 'json',
    contentType: 'application/x-www-form-urlencoded',
    data: {},
    xhr: function() {
        return new XMLHttpRequest();
    },
    timeout: 0
};
exports.get = function() {
    return exports(parseArgs.apply(null, arguments));
};
exports.post = function() {
    var options = parseArgs.apply(null, arguments);
    options.type = 'POST';
    return exports(options);
};
function parseArgs(url, data, success, dataType) {
    if (isFn(data)) {
        dataType = success;
        success = data;
        data = {};
    }
    return {
        url: url,
        data: data,
        success: success,
        dataType: dataType
    };
}
 
module.exports = exports;