您的位置:首页 > 理论基础 > 计算机网络

使用XMLHttpRequest实现AJAx

2016-11-13 19:30 543 查看
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index1.jsp' starting page</title>

    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
window.onload=function(){
//获取节点,为其添加onclick响应函数
  document.getElementsByTagName("a")[0].onclick=function()
  {
    //3:创建一个XMLHttpRequest对象
    var xmlHttp=new XMLHttpRequest();
    //4:准备发送请求的数据:url,设置时间戳,禁用缓存
    var url=this.href+"?time="+new Date();
    var method="GET";
    //5:调用XMLHttpRequest对象的open方法
     xmlHttp.open(method,url);
    //6:调用XMLHttpRequest对象的send方法
    xmlHttp.send(null);
    //7:为XMLHttpRequest对象添加onreadystatechange响应函数
    xmlHttp.onreadystatechange=function(){
    //8:判断响应是否完成:XMLHttpRequest对象的onreadystate属性值是否是4的时候
     if(xmlHttp.readyState==4)
     {
      //9再判断响应属性值是否为200
       if(xmlHttp.status==200||xmlHttp.status==304)
        //10: 打印响应的结果
       alert(xmlHttp.responseText);
     }
    }
    //2:取消节点的默认行为
     return false;
  }
}
</script>

  </head>

  <body>

    <a href="day1/helloAjax.txt">HelloAjax</a>

  </body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: