封装XmlHttpRequest

function ajax(url, onsuccess, onfail) {
    var xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = function () {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                onsuccess(xmlHttp.responseText);
            }
            else {
                onfail(xmlHttp.status);
            }
        }
    }
    xmlHttp.send();
}

原文地址:https://www.cnblogs.com/mdy41034264/p/3564753.html