不懂为什么没有反应,之前没用过ajax,不知道ajax要不要配置啥东西
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'indes.jsp' starting page
<%@ 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+"/";
%>
<%
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("- "+rs.getString(4)+"
");
}while(rs.next());
}else{
out.print("暂无信息");
}
} catch (SQLException e) {
e.printStackTrace();
}
%>
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);
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
闭关修行中......