ajax原生实现

时间:2023-03-09 04:52:48
ajax原生实现
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
  function sendRequest(){
    //js的Ajax访问
    //1)创建Ajax引擎对象
    var xmlhttp = new XMLHttpRequest();
    //2)为Ajax引擎对象绑定监听(监听服务器已将数据响应给引擎)
    xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readState == 4 && xmlhttp.status == 200){
        //5)接收响应数据
        //获得服务器端返回给引擎对象的数据
        alert(xmlhttp.responseText);
      }
    }
    //3)绑定提交地址
    //请求方式:GET
    //地址:url
    //同异步:true异步,false同步
    xmlhttp.open("GET", "AjaxDemo/ajaxServlet", true);
    //4)发送请求
    xmlhttp.send();
  }
</script>
</head>
<body>
  <input type="button" value="ajax异步访问服务器端" onclick="sendRequest()">
</body>
</html>

                                                                2019-04-26

                                                              再完美的程序都会存在漏洞!

                                                                     ——深海藏有溺亡之鱼