ajax实现异步请求

<!DOCTYPE html>
<html lang = "zh-CN">
<head>
    <meta charset = "UTF-8">
    <title>ajax实现异步请求</title>
</head>
<body>
    <button type="button" id="button">异步请求发送按钮</button>
    <script>
        var elementById = document.getElementById("button");

        elementById.addEventListener("click", function (event) {
            // 创建 XMLHttpRequest 对象,检测 IE5、IE6的兼容性问题
            var xml_http = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

            // 请求类型  URI地址  异步请求
            xml_http.open("GET", "/WebSecond_war_exploded/hello", true);

            // 发送请求
            xml_http.send();

            xml_http.onreadystatechange=function()
            {
                if (xml_http.readyState==4 && xml_http.status==200)
                {
                    var responseText = xml_http.responseText;
                    alert(responseText);
                }
            }
        })
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/yumoblogs/p/14969667.html