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文件里.
...全文
138 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥随风翼 2010-11-24
  • 打赏
  • 举报
回复
虽然没人回答,但我自己搞定了,还是谢谢大家帮忙!平均分!
逍遥随风翼 2010-11-23
  • 打赏
  • 举报
回复
逍遥随风翼 2010-11-23
  • 打赏
  • 举报
回复
再次救命啊!经我调试,其它都搞定了,就差最后一哆嗦了!
从checkUser.jsp返回来的东西不是1和0,而是:
就是它把JSP里面的HTML代码一起返回来了,所以最后判断不了。但是我看网上其它代码都是用println()直接输出就可以判断啊???这个怎么解决啊???
daomei_2001_0 2010-11-23
  • 打赏
  • 举报
回复
既然以前用DWR,为什么不能继续用DWR来实现?
lovebaylong 2010-11-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ymf007 的回复:]
求大神,我还没搞定~来个调试方法先~
[/Quote]

在firefox下调试一下你写的脚本,装个firebug插件,就可调试脚本了。。。。
逍遥随风翼 2010-11-19
  • 打赏
  • 举报
回复
求大神,我还没搞定~来个调试方法先~
逍遥随风翼 2010-11-18
  • 打赏
  • 举报
回复
不好意思啊,正则表达式还没学。。回7楼的,=号写了的,COPY过来的时候忘记了。。
tanwan 2010-11-18
  • 打赏
  • 举报
回复
var url='http://localhost:8081/OntoWeb/Information/checkUser.jsp?username'+username;

这里应该改

var url='http://localhost:8081/OntoWeb/Information/checkUser.jsp?username='+username;
风影萧诺 2010-11-18
  • 打赏
  • 举报
回复
也是一个初学者, 了解一下。。。。
Jlins 2010-11-18
  • 打赏
  • 举报
回复
+[Quote=引用 1 楼 zhuchao_ko 的回复:]
我看到你后台out.println(0) 1 2 我就知道 你前台
var returnvalue1=httpRequest1.responseText;
的 returnvalue1 永远判断不到 0 1 或 2
因为你 out.println()出来的东西 不只是只有 0 ,1 或 2
左右还有其它你看不到的字符
你改
var returnvalue1=httpRequest1.r……
[/Quote]
a619225471 2010-11-18
  • 打赏
  • 举报
回复
不好意思 看错了
a619225471 2010-11-18
  • 打赏
  • 举报
回复
out.println(0) 这里里面包含了一个空格
out.print(0) 这里没空格
myhope88 2010-11-18
  • 打赏
  • 举报
回复
楼上说得没错,应该是ajax请求过来的数据不纯导致的,你可以先把它输出看一下是不是正确的
宁波朱超 2010-11-18
  • 打赏
  • 举报
回复
我看到你后台out.println(0) 1 2 我就知道 你前台
var returnvalue1=httpRequest1.responseText;
的 returnvalue1 永远判断不到 0 1 或 2
因为你 out.println()出来的东西 不只是只有 0 ,1 或 2
左右还有其它你看不到的字符
你改
var returnvalue1=httpRequest1.responseText;

var returnvalue1=httpRequest1.responseText.replace(/(^\s*)|(\s*$)/g,"");
这样你前台的 returnvalue1 才能获得 0 1 或 2

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧