liuguangyin

使用原始的javascript实现ajax

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript">

        function run() {

            var xmlHttpReq = null;
            if (window.ActiveXObject) {
                //IE5,IE6是以ActiveXObject的方式引入XMLHttpRequest对象的
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } else if (window.XMLHttpRequest) {
                //除IE5和IE6以外的浏览器
                //XMLHttpRequest是window的子对象
                实例化一个XMLHttpRequest对象
                xmlHttpReq = new XMLHttpRequest();
            }

            xmlHttpReq.open("get", "Handler1.ashx", true);
            
            //一旦readyState值改变,将会调用该函数
            xmlHttpReq.onreadystatechange = function () { //设置回调函数
                if (xmlHttpReq.readyState==4) {
                    if(xmlHttpReq.status==200){
                        alert(xmlHttpReq.responseText);
                        document.getElementById(\'h3\').innerHTML = xmlHttpReq.responseText;

                    }
                }
            }
            
            //因为使用get方法提交,这里可以使用null作为参数调用。
            xmlHttpReq.send(null);//在IE11下不用写null也调用成功了。
        }
    </script>
</head>
<body>
    <input type="button" value="ajax提交" onclick="run();" />
    <h3 id="h3"></h3>
</body>
</html>

分类:

技术点:

相关文章:

  • 2021-09-10
  • 2021-05-20
  • 2022-02-08
  • 2022-02-08
  • 2022-02-08
  • 2021-12-22
  • 2022-12-23
猜你喜欢
  • 2021-09-15
  • 2021-10-15
  • 2021-11-05
  • 2021-12-02
  • 2021-11-06
  • 2021-06-22
相关资源
相似解决方案