Ajax问题,救命啊!!!
逍遥随风翼 2010-11-17 11:53:19 第一次用AJAX做东西,开始准备JQUERY+AJAX的,但是JQUERY要重新学语法,时间确实不多,所以就只用AJAX做一个用户名是否重复的验证了。
本来是很简单的,但是网上很多看得模棱两可,师兄也没用过,以前只用DWR,所以有些艰难.我想要实现的是失去焦点就触发AJAX后台检验,然后在输入框后面显示相应提示信息,先把一些核心代码附上:
function CreateXmlHttp1()
{
var httpRequest1=false;
if(window.XmlHttpRequest)
{
httpRequest1=new XmlHttpRequest();
if(httpRequest1.overrideMimeType)
{//设置MiME 类别
httpRequest1.overrideMimeType("text/xml");
}
}
//IE浏览器创建XmlHttpRequest对象
if(window.ActiveXObject)
{
try
{
httpRequest1=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
try{
httpRequest1=new ActiveXObject("msxml2.XMLHTTP");
}
catch(ex){}
}
}
if(!httpRequest1)
{
alert("创建xmlhttpRequest发生异常!");
return false;
}
else
{
var username=document.getElementById("Reusername").value;
var url='http://localhost:8081/OntoWeb/Information/checkUser.jsp?username'+username;
httpRequest1.open("POST",url,true);
httpRequest1.onreadystatechange=processing
httpRequest1.send(null);
}
}
此函数是初始化AJAX函数
<%
String username=request.getParameter("username");
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mathdata";
Connection con = DriverManager.getConnection(url,"root","2311848");
Statement st = con.createStatement();
String sql = "SELECT UserName from t_user_info WHERE UserName='"+username+"'";
ResultSet rs = null;
rs = st.executeQuery(sql);
if (rs.next())
out.print("0");
else
out.print("1");
}
catch(Exception e)
{
e.printStackTrace();
}
%>
这一段是checkUser.jsp的核心代码
function processing()
{
if(httpRequest1.readystate==4)
{
if(httpRequest1.status==200)
{
var returnvalue1=httpRequest1.responseText;
if(returnvalue1=="1")
{
alert("1");
//document.getElementById("name_blank4").style.display='block';
//document.getElementById("name_blank").style.display='none';
//document.getElementById("name_blank2").style.display='none';
//document.getElementById("name_blank3").style.display='none';
}
else
{
alert("0");
//document.getElementById("name_blank3").style.display='block';
//document.getElementById("name_blank").style.display='none';
//document.getElementById("name_blank2").style.display='none';
// document.getElementById("name_blank4").style.display='none';
}
}
}
}
这一段是FWQ返回结果之后的处理函数.
function nametest()
{
var i,strlengh,tempchar,strlength;
var str=document.getElementById("Reusername").value;
if(str==""||str==null)
{
document.getElementById("name_blank").style.display='block';
document.getElementById("name_blank2").style.display='none';
document.getElementById("name_blank3").style.display='none';
document.getElementById("name_blank4").style.display='none';
}
else if(true)
{
strlength=str.length;
for(i=0;i<strlength;i++)
{
tempchar=str.substring(i,i+1);
if(strlength>16||!(tempchar=='0'||tempchar=='1'||tempchar=='2'||tempchar=='3'||tempchar=='4'||tempchar=='5'||tempchar=='6'||tempchar=='7'||tempchar=='8'||tempchar=='9'||
tempchar=='_'||tempchar=='a'||tempchar=='A'||tempchar=='b'||tempchar=='B'||tempchar=='c'||tempchar=='C'||tempchar=='d'||tempchar=='D'||tempchar=='e'||
tempchar=='E'||tempchar=='f'||tempchar=='F'||tempchar=='g'||tempchar=='G'||tempchar=='h'||tempchar=='H'||tempchar=='i'||tempchar=='I'||tempchar=='j'||
tempchar=='J'||tempchar=='k'||tempchar=='K'||tempchar=='l'||tempchar=='L'||tempchar=='m'||tempchar=='M'||tempchar=='n'||tempchar=='N'||tempchar=='o'||
tempchar=='O'||tempchar=='p'||tempchar=='P'||tempchar=='q'||tempchar=='Q'||tempchar=='r'||tempchar=='R'||tempchar=='s'||tempchar=='S'||tempchar=='t'||
tempchar=='T'||tempchar=='u'||tempchar=='U'||tempchar=='v'||tempchar=='V'||tempchar=='w'||tempchar=='W'||tempchar=='x'||tempchar=='X'||tempchar=='y'||
tempchar=='Y'||tempchar=='z'))
{
document.getElementById("name_blank2").style.display='block';
document.getElementById("name_blank1").style.display='none';
document.getElementById("name_blank3").style.display='none';
document.getElementById("name_blank4").style.display='none';
break;
}
}
}
else
{
CreateXmlHttp1();
}
}
这一段函数就是失去焦点的触发函数了,我不知道里面的写法对不对,开始没有验证用户名是否重复的时候这个函数是有效的,但是现在没啥作用,我有点怀疑是用了if(true)的原因.注:tomcat端口改成了8081,以上3个JS函数写在一个JS文件里.