AJAX的基本函数

来源:互联网 发布:excel怎样数据保护 编辑:程序博客网 时间:2024/06/06 02:20

这仅仅是自己的学习笔记,只为记录自己的成长。


<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Ajax</title>    <script>        /*function loadXMLDoc(){            //创建 XMLHttpRequest对象            var xmlhttp;            if(window.XMLHttpRequest){                xmlhttp = new XMLHttpRequest();            }else{                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");            }            xmlhttp.onreadystatechange=function(){                if(xmlhttp.readyState==4 && xmlhttp.status==200){                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;                }            }            //向服务器发送请求            xmlhttp.open("GET","test1.txt",true);                        //open(method,url,async)            //    method: 请求的类型,GET或POST            //    url:文件在服务器上的位置            //    async: true异步,false同步                        xmlhttp.send();        }*/        //Callback函数,是一种以参数形式传递给另一个函数的函数        //如果网站上存在多个AJAX任务,那么应该为创建XMLHttpRequest对象编写一个标准的函数,并为        //每个AJAX任务调用该函数。        //该函数调用应该包含url以及发生onreadystatechange事件时执行的任务        function loadXMLDoc(url,cfunc){            if(window.XMLHttpRequest){                xmlhttp = new XMLHttpRequest();            }else{                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");            }            xmlhttp.onreadystatechange=cfunc;            xmlhttp.open("GET",url,true);            xmlhttp.send();        }        function    myFunction(){            loadXMLDoc("test1.txt",function(){                if(xmlhttp.readyState==4 && xmlhttp.status==200){                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;                }            })        }    </script></head><body>    <h2>AJAX</h2>    <button type="button" onclick="myFunction()">请求数据</button>    <div id="myDiv"></div></body></html>


原创粉丝点击