在脚本中要执行一个后台的方法适合我们就用一般处理文件
例如:注册检测用户名是否可用
<script type="text/javascript" language="javascript">
$(document).ready(function() {
//验证用户名
$('#txtuserName').change(function() {
var UserName = document.getElementById("txtuserName").value;
$.get("/Ajax/IsExistUser.ashx", { uname: UserName }, function(result) {
if (result == "用户名" + UserName + "已经被使用,请重新输入!") {
$("#txtuserName").css({ border: "1px solid red" });
document.getElementById("spanUserName").innerHTML = "用户名" + UserName + "已经被使用,请重新输入!";
$("#txtuserName").focus();
}
else if (UserName.length > 4) {
if (result == "恭喜您,用户名 " + UserName + " 可以使用!") {
$("#txtuserName").css({ border: "1px solid #c4d2db" });
document.getElementById("spanUserName").innerHTML = "恭喜您,用户名 " + UserName + " 可以使用!";
$("#spanUserName").css({ color: "#666699" });
}
}
});
});
});
</script>
IsExistUser.ashx文件内容为:
context.Response.ContentType = "text/plain";
Entity.MemberInfo model = new Ticket.Entity.MemberInfo();
string UserName = Common.Request.GetString("uname", true);
model.UserName = UserName;
context.Response.StatusCode = 200;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
model = Logic.MemberLogic.GetModel(model, out errMsg);
if (!string.IsNullOrEmpty(UserName))
{
if (model == null)
{
if (UserName == "-1")
{
context.Response.Write("请勿输入非法字符");
}
else
{
context.Response.Write(string.Format("恭喜您,用户名 {0} 可以使用!", UserName));
}
}
}
例子2:点击标题 新的层中出来相应的内容
//公告显示与隐藏DIV
function doNotice(obj) {
var divNTC = document.getElementById("divNotice");
if (divNTC.style.display == "none") {
divNTC.style.display = "";
spanNoticeTitle.innerHTML = obj.innerHTML;
$.ajax({
type: "post",
url: "/Ajax/NoticeHandler.ashx",
data: "id=" + obj.title,
success: function(msg) {
$("#spanNoticeContext").html("<p style='text-align:left;margin:5px 5px 5px 5px;'> " + msg + "</p>");
}
});
} else {
divNTC.style.display = "none";
}
}
<a href="#" οnmοusedοwn="doNotice(this)" title='<%#Eval("PeopleId")%>'>
<%#Eval("Title")%></a>
public void ProcessRequest(HttpContext context)
{
int errMsg = -1;
JinRi.Ticket.Entity.NoticeInfo notice = new JinRi.Ticket.Entity.NoticeInfo();
string id = JinRi.Ticket.Common.Request.GetString("id");
if (!string.IsNullOrEmpty(id))
{
notice.PeopleId = Convert.ToInt32(id);
notice = JinRi.Ticket.Logic.NoticeLogic.GetModel(notice, out errMsg);
}
if (errMsg != -1)
{
context.Response.Write(errMsg);
}
else
{
context.Response.Write(notice.Context);
}
}