ajax.js 1.0 KB
Newer Older
Y
ylwdev 已提交
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
window.AJAX = function (opt) {
    opt = Object.assign({}, {
        type: 'POST',
        async: true,
        isJson: true
    }, opt || {});
    let xhr, data;
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE 6 and older
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (opt.isJson) {
        data = JSON.stringify(opt.data);
    }

    xhr.onreadystatechange = function () {
        // try {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                let res = xhr.responseText;
                opt.isJson && (res = JSON.parse(res));
                opt.success && opt.success(res);
            } else {
                console.log('There was a problem with the request.');
            }
        }
        // } catch (e) {
        //     console.error('Caught Exception: ' + e);
        // }
    }
    xhr.open(opt.type, opt.url, opt.async);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(data);
}