zoukankan      html  css  js  c++  java
  • Ajax_使用XMLHttpRequest实现

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8" />
     5     <title>Document</title>
     6     <script type="text/javascript">
     7     window.onload = function () {
     8         // 1.获取a节点,并为其添加 onclick 相应函数
     9         document.getElementsByTagName("a")[0].onclick = function(){
    10             //3.创建一个XMLHttpRequest 对象
    11             var request = new XMLHttpRequest();
    12 
    13             //4.准备发送请求的数据:url
    14             var url = this.href + "?time=" + new Date();
    15             var method = "GET";
    16 
    17             //5.调用XMLHttpRequest 对象的 open 方法
    18             request.open(method, url);
    19 
    20             //6.XMLHttpRequest 对象的 send 方法
    21             request.send(null);
    22 
    23             //7.为XMLHttpRequest 对象添加 onreadystatechange 相应函数,由服务器触发
    24             request.onreadystatechange = function () {
    25 
    26                 //8.判断响应是否完成:XMLHttpRequest 对象的 readyState 属性值为 4 的时候
    27                 if (request.readyState == 4) {
    28                     //9.再判断响应是否可用: XMLHttpRequest 对象 status 属性值为 200
    29                     if (request.status == 200 || request.status ==304) {
    30                     //10.打印响应结果:responseText
    31                     alert(request.responseText);
    32                     }
    33                 }
    34             }
    35 
    36             //2.取消a的默认行为
    37             return false;
    38         }
    39     }
    40     </script>
    41 </head>
    42 <body>
    43     <a href="hellpAjax.txt">hellpAjax</a>
    44 </body>
    45 </html>
    我的个人博客,欢迎来访问!网址:http://www.miuu.club
  • 相关阅读:
    javascript cookie
    mark几个比较好的配色网站
    Javascrip 淡入淡出思路
    实验报告:统计字符串中子字符串出现的次数
    Javascript计算器
    《node入门》学习
    配置ionic(低版本)
    eclipse环境配置
    关于文档加载的方法
    javascript基础-《web前端最佳实践》
  • 原文地址:https://www.cnblogs.com/yu520zhong/p/4844008.html
Copyright © 2011-2022 走看看