首先写一个一般处理程序来获取到你要加载到前台的数据,并序列化成json格式。
//代码实例public class AjaxUserList : IHttpHandler {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/plain"
BLL.UserInfoBll userInfoBll = new BLL.UserInfoBll()
int pageIndex
if (!int.TryParse(context.Request["pageIndex"],out pageIndex)) {
pageIndex = 1
}
int pageSize = 5
int pageCount = userInfoBll.GetPageCount(pageSize)
//判断当前页码的取值范围
pageIndex = pageIndex < 1 ? 1 : pageIndex
pageIndex = pageIndex > pageCount ? pageCount : pageIndex
//获取分页数据
List<Model.UserInfo> userList = userInfoBll.GetPageList(pageIndex, pageSize)
//获取页码条
string pageBar = Util.PageBar.GetPageBar(pageIndex, pageCount)
//***********************************************************
//使用匿名类将多组数据序列化成Json格式
//***********************************************************
JavaScriptSerializer js = new JavaScriptSerializer()
string json = js.Serialize(new { resultUserList = userList, resultPageBar = pageBar })//此处使用了匿名类将userList集合和pageBar进行封装后,再序列化
//***********************************************************
context.Response.Write(json)
}
public bool IsReusable {
get {
return false
}
}
}
然后,前台ajax请求这个一般处理处理程序获取到json数据,再通过js将数据添加到html。
//加载用户列表示例function LoadUserInfo(pageIndex) {
$.post("AjaxUserList.ashx", { "pageIndex": pageIndex }, function (data) {
var serverData = $.parseJSON(data)
for (var i = 0 i < serverData.resultUserList.length i++) {
$("<tr><td>" + serverData.resultUserList[i].ID + "</td><td>" + serverData.resultUserList[i].UName + "</td><td>" + serverData.resultUserList[i].UPwd + "</td><td>" + ChangeDateFormat(serverData.resultUserList[i].SubTime) + "</td><td>" + serverData.resultUserList[i].Remark + "</td><td><a href='javascript:void(0)' class='details' Uid='" + serverData.resultUserList[i].ID + "'>详细</a></td><td><a href='javascript:void(0)' class='edits' Uid='" + serverData.resultUserList[i].ID + "'>编辑</a> <a href='javascript:void(0)' class='deletes' Uid='" + serverData.resultUserList[i].ID + "' UName='" + serverData.resultUserList[i].UName + "'>删除</a></td></tr>").appendTo("#tabUserList")
}
})
}
$.ajax({\x0d\x0atype:'POST',url:'submitcomment.php',timeout:2000,cache:true,\x0d\x0adata:'dataid='+dataid+'&appid='+appid+'&name='+name.val()+'&txt='+txt.val()+'&openid='+openid,\x0d\x0aerror:function(msg){return false},\x0d\x0asuccess:function(json, st){\x0d\x0aif(json.result == 0){\x0d\x0aalert('您的评论审核通过后,才会显示出来!')\x0d\x0a}else{ //否则审核通过,显示出来\x0d\x0a$("#comlistx0014jwh62k").html(json)\x0d\x0a}\x0d\x0a}\x0d\x0a})\x0d\x0a \x0d\x0a后台submitcomment.php代码:\x0d\x0a \x0d\x0a欢迎分享,转载请注明来源:内存溢出
评论列表(0条)