码迷,mamicode.com
首页 > Web开发 > 详细

ajax初识

时间:2017-07-28 00:24:31      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:ref   return   获取   response   XML   utf-8   body   ajax   func   

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title></title>
</head>
<script type="text/javascript">
  window.onload = function() {
  //1:获取a节点,并对其添加onclick()相应函数
  document.getElementsByTagName("a")[0].onclick = function() {
  // 2:创建一个XMLHttpRequest对象
    var request = new XMLHttpRequest();
    // 3:准备发送请求的数据:url
    var url = this.href;
    //4:调用XMLHttpRequest对象的open方法
    request.open("GET", url);
    //5:调用XMLHttpRequest对象的send方法

    request.send(null);
    //6:为XMLHttpRequest对象添加Onreadystatechange响应函数
    request.onreadystatechange = function() {
      //判断响应是否完成:XMLHttpRequest的readyState的属性值为4的时候
      // alert(request.readyState);
      if(request.readyState == 4) {
      // 判断响应是否可用,XMLHttpRequest的status的属性值为200的时候

         if(request.status == 200 || request == 304){
          //8:打印响应结果
          alert(request.responseText);
        }


      }
    }
  return false; //保证不会调转啊,取消点击的默认行为
  };
}
</script>
</head>

<body>
<a href="helloAjax.txt">helloAjax</a>
</body>

</html>

 上述实现默认a链接不跳转,get方法读取hello_world.txt内部内容

*POST:加 request.setRequestHeader("ContentType","application/x-www-form-urlencoded");

ajax初识

标签:ref   return   获取   response   XML   utf-8   body   ajax   func   

原文地址:http://www.cnblogs.com/infernoyy/p/7247947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!