ajax没有反应

新手上路,请多包涵

不懂为什么没有反应,之前没用过ajax,不知道ajax要不要配置啥东西

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'indes.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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
  <script type="text/javascript" src="AjaxRequest.js"></script>
   <script type="text/javascript">
   function onerror()
   {alert("操作错误");}
   
   function getInfo()
   {
    var loader=new net.AjaxRequest("getInfo.jsp?nocache="+new Date().getTime() , deal_getInfo, onerror, "GET"); 
   }
   
   function deal_getInfo()
   {
   document.getElementById("showInfo").innerHTML=this.reg.responseText;
      window.setInterval("getInfo()", 1000);
   }
  
   </script>

  
<body onload="getInfo()">
  
   <div style="border: 1px solid;height:50px; width:200px;padding:5px;">
  
   <div id="showInfo"></div>
  
   </div>
  

  </body>
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</html>



<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" import ="web.DB"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

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



  <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<body>
<ul>
<% 

Connection conn=DB.getConnction();
PreparedStatement pst=null;
ResultSet rs=null;
String sql="select top 1 * from db_user order by id desc";

try{
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            
            if(rs.next())
            {
                do{
                out.print("<li>"+rs.getString(4)+"</li>");
                
                
                }while(rs.next());
            }else{
            out.print("暂无信息");
            
            }
            
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        
 
 
 %>
</ul>
  </body>
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</html>

var net=new Object(); //定义一个全局变量net
//编写构造函数
net.AjaxRequest=function(url,onload,onerror,method,params){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadDate(url,method,params);
}
//编写用于初始化XMLHttpRequest对象并指定处理函数,最后发送HTTP请求的方法
net.AjaxRequest.prototype.loadDate=function(url,method,params){
  if (!method){ //设置默认为的请求方式为GET
  method="GET";
  }
  if (window.XMLHttpRequest){ // Mozilla……等非IE浏览器
  this.req=new XMLHttpRequest(); //创建XMLHttpRequest对象
  } else if (window.ActiveXObject){ //IE浏览器
try{
this.req=new ActiveXObject("Msxml2.XMLHTTP"); //创建XMLHttpRequest对象
}catch(e){
try{
this.req=new ActiveXObject("Microsoft.XMLHTTP"); //创建XMLHttpRequest对象
}catch(e){}
}
  }
  if (this.req){
  try{
  var loader=this;
  //指定回调函数
  this.req.onreadystatechange=function(){
  net.AjaxRequest.onReadyState.call(loader);
  }
  this.req.open(method,url,true); //创建与服务器的连接
if(method=="POST"){ //当发送POST请求时,设置请求头
this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
}
  this.req.send(params); //向服务器发送请求
  }catch (err){
  this.onerror.call(this); //调用错误处理函数
  }
  }
}

//重构回调函数
net.AjaxRequest.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState; //获取请求状态
  if (ready==4){
  if (req.status==200 ){
  this.onload.call(this); //调用回调函数
  }else{
  this.onerror.call(this); //调用错误处理函数
  }
  }
}
//重构默认的错误处理函数
net.AjaxRequest.prototype.defaultError=function(){
  alert("错误数据\n\n回调状态:"+this.req.readyState+"\n状态: "+this.req.status);
}



阅读 3.7k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题