jquery调用ashx文件和WebMethod方法总结
//.........................................................JQuery调用ashx文件..................................................................
<script type="text/javascript">
$(function() {
$("#txtName").blur(function() {
JudgeUserName();
});
});
function JudgeUserName()
{
$.ajax({
type:"GET",
url: "Handler.ashx",
dataType:"html",
data:{"userName":$("#txtName").val(),"action":"bbb"},
beforeSend:function(XMLHttpRequest)
{
$("#showResult").html("正在查询");
},
success:function(msg)
{
$("#showResult").html(msg);
$("#showResult").css("color","red");
},
complete:function(XMLHttpRequest,textStatus)
{
//隐藏正在查询图片
},
error:function()
{
//错误处理
}
});
}
</script>
Handler.ashx文件
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string action = context.Request.QueryString["action"].ToString();
if (action == "bbb")
{
context.Response.Write(Result(context));
}
}
public bool IsReusable {
get {
return false;
}
}
public string Result(HttpContext context)
{
string userName = context.Request.QueryString["userName"].ToString();
if (userName == "James Hao")
{
return "用户名已经存在!";
}
else
{
return "您可以使用此用户名!";
}
}
//...................................................................JQuery调用WebMethod方法.........................................................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> </style>
} //返回DataSet(XML) //Ajax 为用户提供反馈,利用ajaxStart和ajaxStop 方法,演示ajax跟踪相关事件的回调,他们两个方法可以添加给jQuery对象在Ajax前后回调 // 鼠标移入移出效果,多个元素的时候,可以使用“,”隔开 </head> using System; public partial class Default2 : System.Web.UI.Page } while (i >= 0) return list; /// <summary>
} |