纯手写ajax

function loadAsyncData(url, id) {
    var xmlhttp;
    if (!url || !id) {
        return;
    }
    domId = id;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null) {
        try {
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {// 4 = "loaded"数据接收完毕,此时可以通过通过responseBody和responseText获取完整的回应数据
                    if (xmlhttp.status == 200) {// 200 = "OK"
                        document.getElementById(id).innerHTML = xmlhttp.responseText;
                    }
                    else {
                        document.getElementById(id).innerHTML = ''; //default ad
                    }
                }
            }


            xmlhttp.open('GET', url, true);

            xmlhttp.send(null);


        } catch (e) {
            alert(e);
        }
    }
    else {
        alert("Your browser does not support XMLHTTP.");
    }
}
function aa() {
    loadAsyncData("Hand.ashx?id=" + document.getElementById('Text1').value, "hh");
    document.getElementById("hh").style.display = "block";
}
原文地址:https://www.cnblogs.com/lilyzhang/p/1585084.html