ajax无刷新评论的思路,ajax学习——ajax版无刷新评论(数据库)

//Comment.htm

无刷新评论

type="text/javascript">

//加载评论

$(function() {

$.post("GetComment.ashx",

function(data, status) {

if (status != "success") {

$("#ulComment").append($("

加载数据失败"));

return;

}

var lines =

data.split("$");

for (var i = 0; i < lines.length; i++) {

var line = lines[i];

var part = line.split("|");

var comment = $("

/>

用户ip:"

+ part[0] + "

/>评论内容:" + part[1] +

"

/>时间:" + part[2] +

"

");

$("#ulComment").append(comment);

}

})

})

//评论

$(function() {

$("#btnPost").click(function()

{

var txtComment =

$("#txtComment").val();

$.post("SetComment.ashx", {

"comment": txtComment },

function(data, status) {

if (status != "success") {

alert("评论失败,请重新评论");

return;

}

var lines =

data.split("|");

if (lines[0] == "ok") {

alert("谢谢评论");

var Comment = $("

/>

用户ip:"

+ lines[1] + "

/>评论内容:" + txtComment +

"

/>时间:" + lines[2] +

"

");

$("#ulComment").append(Comment);

$("#txtComment").val("");//清除文本框内信息

}

else {

alert("数据错误");

}

})

})

})

style="width: 662px"

>

style="list-style-type:none">北京梁蓓委员谈80后无力应对高房价的建议:我觉得80后男孩子如果买不起房子,80后女孩子可以嫁给

40岁的男人。80后的男人如果有条件了,到40岁再娶20岁的女孩子也是不错的选择。

id="txtComment"

cols="40"

rows="4">

/>

type="button"

id="btnPost"

value="提交评论"

/>

//SetComment.ashx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using ajax无刷新评论.DS_CommentTableAdapters;

namespace ajax无刷新评论

{

///

///

评论导入到数据库,并返回数据

///

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class

SetComment : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType =

"text/plain";

string comment =

context.Request["comment"];

new

T_CommentTableAdapter().InsertQuery(context.Request.UserHostAddress,comment,DateTime.Now);

context.Response.Write("ok|"+context.Request.UserHostAddress+"|"+DateTime.Now);

}

public bool IsReusable

{

get

{

return false;

}

}

}

}

//GetComment.ashx

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using ajax无刷新评论.DS_CommentTableAdapters;

using System.Text;

namespace ajax无刷新评论

{

///

///

数据库中的数据送到页面

///

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class

GetComment : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType =

"text/plain";

var comments = new T_CommentTableAdapter().GetData();

StringBuilder str = new StringBuilder();//字符串,很给力的字符串

foreach (var comment in comments)

{

str.Append(comment.IP).Append("|").Append(comment.Comment).Append("|").Append(comment.DateTime).Append("$");

//用"|"隔开每个字段,用"$"隔开每一行

}

context.Response.Write(str.ToString().Trim('$'));//把最后面的$去掉

}

public bool IsReusable

{

get

{

return false;

}

}

}

}

数据库结构:

a4c26d1e5885305701be709a3d33442f.png

DataSet结构:

a4c26d1e5885305701be709a3d33442f.png

演示截图:

a4c26d1e5885305701be709a3d33442f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值