ASP jQuery AJAX无刷新评论
看代码实现,这里我删减了一部分,便于说明(comments.asp):
$(function(){
//页面初始化时获取评论内容
getComments(1);
});
//获取评论内容
function getComments(pageno){
$.ajax({
type:"GET",
url:"proc_comments.asp",
data:"action=getComments&id=44&page="+pageno+"&num="+Math.round(Math.random()*10000),
error:function(){$("#comm_list table tr td").html("获取评论信息失败");},
success:function(comments_data){
$("#comm_list").html(comments_data);
}
});
}
//添加评论到数据库,同时更新显示,注意escape编码后提交
function addComments(){
if($.formValidator.pageIsValid()){
$.ajax({
type:"POST",
url:"proc_comments.asp",
data:"action=addcomments&id=44&comm_user="+escape($("#comm_user").val())+"&comm_content="+escape($("#comm_content").val()),
success:function(){
getComments(1);
}
});
}
}
这里的$.formValidator.pageIsValid()猫东的表单验证插件的一个表单验证函数,我用button替代了原有的submit,在它的onclick事件中添加addComments()函数提交评论,为避免空提交,这里提交前先做验证。
提交数据是action代表要执行的操作,id传递文章id,page是请求的分页页面,Math.round(Math.random()*10000)防止页面被浏览器缓存而导致暂时无法刷新,如果出错显示错误信息
客户端用escape()函数编码含中文的字符串,服务器端用unescape()解码,这样做是为了防止乱码,如果你采用的是utf8,那就不必了,设置正确的页面编码和服务端脚本编码就不会出问题。由于jquery默认使用utf8传输数据,而且不提供设置编码,这里只有自己做转换,如果你用form插件的时候就会发现,编码问题是不能回绝的,看来以后得向utf8看起了,呵呵!
看以下处理代码,长了点,不过就三个函数,一个分支语句(proc_comments.asp):
<%
response.charset="gb2312"
dim action,id
action=Lcase(trim(Request("action")))
id=trim(Request("id"))
select case action
case "getcomments"
dim pageno
pageno=trim(Request.QueryString("page"))
call getComments(id,pageno)
case "addcomments"
addComments(id)
case else
Response.Write "else"
end select
Function IIf(bExp1, sVal1, sVal2)
If (bExp1) Then
IIf = sVal1
Else
IIf = sVal2
End If
End Function
'操作数据库返回评论内容,包含了分页的显示
function getComments(id,page)
tempstr1="<div class=""comm_item"">" & vbcrlf &_
"<h4>网友:{$comm_user$} 发表于 {$comm_pubdate$}</h4>" & vbcrlf &_
"<div class=""comm_item_body"">{$comm_content$}" & vbcrlf &_
"</div>" & vbcrlf & "</div>" & vbcrlf
tempstr2="<table width=590 height=292 border:1px solid #CCC;margin:5px 5px;""><tr><td align=center>暂无人发表评论</td></tr></table>"
dim rethtml
openConn(dbfile)
sqlstr="select * from comments where comm_pid=" & id & " order by comm_subdate desc"
'sqlstr="select * from comments"
call openRS(sqlstr,1)
dim rscnt:rscnt=rs.recordcount
dim pagesize:pagesize=3
dim pagenum
if (rscnt mod pagesize)=0 then
pagenum=rscnt/pagesize
else
pagenum=Int(rscnt/pagesize)+1
end if
If not(rs.bof and rs.eof) Then
dim swaphtml,cnt
rs.pagesize=3
rs.absolutepage=page
cnt=rs.pagesize
do while not rs.eof and cnt>0
cnt=cnt-1
swaphtml=tempstr1
swaphtml=replace(swaphtml,"{$comm_user$}",rs("comm_user"))
swaphtml=replace(swaphtml,"{$comm_pubdate$}",rs("comm_subdate"))
swaphtml=replace(swaphtml,"{$comm_content$}",rs("comm_content"))
rethtml=rethtml & swaphtml
rs.movenext
loop
else
rethtml=tempstr2
End If
closeRS()
closeConn()
'显示分页相关信息
If rscnt>0 then
pagehtml="<div class=""comm_page"">已有{$rscnt$}条评论 {$link_list$}</div>"
pagehtml=replace(pagehtml,"{$rscnt$}",rscnt)
If pagenum>1 then
for i=1 to pagenum
linkhtml=linkhtml & IIf(i=page,i & " ","<a href=""javascript:getComments(" & i & ")"">" & i & "</a> ")
next
else
linkhtml=""
end if
pagehtml=replace(pagehtml,"{$link_list$}",linkhtml)
rethtml=rethtml & pagehtml
End if
Response.Write rethtml
end function
'处理提交的评论,添加到数据库,注意先unescape解码,否则乱码
function addComments(id)
openConn(dbfile)
sqlstr="insert into [comments](comm_user,comm_content,comm_pid) values('" &_
unescape(trim(Request.Form("comm_user"))) & "','" & unescape(trim(Request.Form("comm_content"))) & "'," & id & ")"
conn.execute(sqlstr)
closeConn()
end function
%>
IIf()是一个简化函数,getComments()返回评论信息,有两个参数,文章id和分页编码,addComments()添加评论,很容易就完成了,源码贴上了:
代码:comments.rar
不赘述了,IE8写文章太麻烦了,编辑器问题太多了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端