紧急求助,刚学ajax,脑袋不行

deng4437 2010-01-24 11:35:35
我原来写了一个聊天,比较土,是用网页自动刷新的,然后今天想把它改成ajax的了

然后呢,在每秒自动刷新局部域上(聊天信息显示面板上)做得蛮成功的,就是后来我在主窗口中加了一个一文本框和一个按扭(用于用户发送聊天记录,然后通过按扭再异步上传去更新数据库的),但是后来就出问题了,
用按钮onclick事件,里面写的代码怎么也和ajax后台服务的asp文件连不起来(那个asp文件在这里就是下面这个文件如下,我一并都写在一起了)

最奇怪的是,为什么我用onload事件连异步后台可以了(说法是每秒刷新的),用onclick就不行?
用get和post都不行

xmlhttp的readystate的值是4(已连接 ),但是status的值确是404,查下来的意思是not found

真郁闷怎么会连不上的,请哪大哥帮忙看看,谢谢啦!急

对了,后来我再用响应click事件的那个xmlhttp对象的responseText打出来竟然是..
系统的文档说明和函数实现,什么是htmlencoder,urlencoder那些方法的实现,还有全部的系统数据格式..我简直昏倒,快哭 出来 了



<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

<%

Response.Charset="gb2312"
%>

<%'if session("username")="" then
'response.Redirect("transition.asp")
'else%>



<%'end if%>


<%

'处理ajax请求,每一秒中更新显示数据库聊天记录到显示面板!(注显示面板也在此asp文件中)

if request.querystring("fordisplay")<>"" then

dim conn,cnnstr,sqlforquery,rs,numberfordisplayeditem
numberfordisplayeditem=30
set conn=server.CreateObject("adodb.connection")
cnnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("chattingroom.mdb")
conn.open cnnstr
set rs=server.CreateObject("adodb.recordset")
sqlforquery="SELECT TOP "&numberfordisplayeditem&" * FROM chatting ORDER BY d desc"
rs.open sqlforquery,conn,1,1

if not rs.eof then

rs.movelast

do while not rs.bof
response.Write rs("d")&" 聊友 "&rs("username")&"说: "&replace(rs("content"),vbcrlf,"<br>")&"<br>"

rs.moveprevious

loop


end if
rs.close
set rs=nothing
conn.close
set conn=nothing
response.write "<a name=""end""></a>"
response.end


%>

<%

'处理ajax请求,更新数据库新聊天记录!

elseif request("forsend")<>"" then




set conn=server.createobject("adodb.connection")
cnnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("chattingroom.mdb")
set rs=server.createobject("adodb.recordset")

'以下一行需要改善,还不完善!请注意! session这边,还有头部需要用判断语句来判断是否是未登录用户直接非法登录了!

mysql="INSERT INTO CHATTING (USERNAME , CONTENT ) VALUES ('"&session("username")&"','"&request("forsend")&"')"
'response.write("<script type=""text/javascript"">alert(""发送成功!"");</script>")
conn.open cnnstr
rs.open mysql,conn,3,3
response.write("我成功了")
set rs=nothing

conn.close
set conn=nothing

end if

%>


<!-- 如下是正常也就是第一次访问该asp产生的html代码,就第一次进入该页面要用到-->
<!-- 它也是显示框架-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>小孩聊天室</title>



</head>
<style type="text/css">
div
{
overflow:auto;
width:1000px;
height:500px;

}

form
{
position:relative;

}
input
{
position:absolute;
top:200px;
left:600px

}
textarea
{
position:absolute;
top:150px;
left:170px;
width:400px;
height:100px;

}

</style>

<body onload="location.href='#end';">
<div id="panel"></div >

<form>
<textarea name="content" cols="11" rows="6"></textarea>
<input type="button" name="send" value="发送" />
<input style="left:650px;" type="button" name="exit" value="注销" />
<span id="prompt" style="position:absolute;left:600px;top:240;"></span>
</form>


</body>


<script type="text/javascript">
var xmlobj=null;
var xmlobj2=null;

window.onload=function(){


last();



//按钮部分
document.getElementsByName("send")[0].onclick=function(){

var r = new RegExp("\\s*\\S+\\s*","g");


if(document.getElementsByName("content")[0].value && r.test(document.getElementsByName("content")[0].value))
{

xmlobj2 =operation("display.asp?forsend="+document.getElementsByName("content")[0].value,"get",forsend);
xmlobj2.setRequestHeader("If-Modified-Since","0");
xmlobj2.send(null);

}
else
{
alert("不能留空或者输入空白字符,请重新输入!");
document.getElementsByName("content")[0].value="";
document.getElementsByName('content')[0].focus();

}





}


}


function forchange()
{

if (this.readyState == 4 )
{

var panel = document.getElementById("panel");
//alert(xmlobj.responseText);
panel.innerHTML=this.responseText;
location.href="#end";
setTimeout("last();",500);





}

}

function forsend()
{
if(this.readyState ==1 ||this.readyState == 2 || this.readyState == 3)
{
document.getElementById("prompt").innerHTML="正在发送...";

}
else if (this.readyState == 4)
{

document.getElementById("prompt").innerHTML="";

}

}

function operation(url,way,method)
{
var temp;
if(window.XMLHttpRequest)
{
temp=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{

temp=new ActiveXObject("Msxml2.XMLHTTP");
}

catch(e)
{

try{
temp=new ActiveXObject("Microsoft.XMLHTTP")
}

catch(e)
{
alert("你的浏览器不支持Ajax!");

}


}
}

if(temp)
{

temp.onreadystatechange=method;
temp.open(way,url,true);
return temp;
}


}


function last()
{

xmlobj=operation("display.asp?fordisplay=yes","get",forchange);
xmlobj.setRequestHeader("If-Modified-Since","0");
xmlobj.send(null);


}

</script>
</html>
...全文
147 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhousq00 2010-01-25
  • 打赏
  • 举报
回复
404的话
你直接运行后台页面看一下
它会报错,看一下什么错就知道了!
itliyi 2010-01-25
  • 打赏
  • 举报
回复
看能成不


var xmlHttp;
function createXmlRequest()
{
if(window.ActiveXObject) {
var xmlHttps=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp4.0", "MSXML2.XMLHttp3.0","MSXML2.XMLHttp","Microsoft.XMLHTTP"]; try {
for(var i=0;i<xmlHttps.length;i++) {
var xmlHttp=new ActiveXObject(xmlHttps[i]);
return xmlHttp;
}
} catch (error) { }
} else {
var xmlHttp=new XMLHttpRequest();
return xmlHttp;
}
throw new Error("无法创建xmlhttprequest对象");
}
function showMes(){
xmlHttp=createXmlRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","Handler.ashx",true); xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
document.getElementById("divMes").innerHTML=xmlHttp.responseText;
}
}
</script>
<body onload="setTimeOut('showMes()',1000);">


Handler.ashx:

SqlConnection ...
SqlCommand cmd=...
SqlDataReader dr=cmd.ExecuteReader();
String returnValue="<table>";
if(dr.HasRow)
{
While(dr.Read())
{
returnValue+="<tr><td>"+dr["mes"].ToString()+"</td></tr>";
}
}
returnValue+="<table>";
context.Response.write(returnValue);context.Response.Flush();

草根醉秋意 2010-01-25
  • 打赏
  • 举报
回复
路径不对啊貌似
happy664618843 2010-01-25
  • 打赏
  • 举报
回复
404路径有问题 找下原因
itliyi 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 andrewsway 的回复:]
代码好长,帮顶
[/Quote]e
andrewsway 2010-01-25
  • 打赏
  • 举报
回复
代码好长,帮顶
zhaoyongqiangri 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ltnrain 的回复:]
路径不对啊貌似
[/Quote]
同感!404
deng4437 2010-01-25
  • 打赏
  • 举报
回复
不行啊..
是不是一个页面内只有一个组件在进行ajax连接啊?
我这个留言本里面把发送信息(onlick)和每秒刷新留言版(用setTimeout一秒定时的)都写在里面了

那为什么onclick那个响应就不行连不上了?
我试过了,不是我的后台那个响应页面的问题,因为我如果放回我原来我写的那个里面还是可以运行的,我原来是做成分离型的聊天面板的,就是用框架做的
发送和显示聊天信息不是在一个文件内,所以那时候是绝对没问题的,后来我把发送功能和显示合并在一个页面内想用ajax交互却不行了,问题出在onclick那个局部刷新连不起来,status是404

52,797

社区成员

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

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